The release of the copy structure feature between languages was extremely helpful. Easely manage complex multi-language site with DatoCMS
On a weekly basis we still encounter the desire to copy a content entry from one model to another model. Is this on your roadmap?
I was thinking this could be part of the context menu for a block, but doesn’t have to be.
This would be a great idea. In case others would like to add to this, we have a very raw base that copies and pastes blocks from one page to another, but the GUI that would allow users to do this without Graphql query is still missing. Also, this code is not refactored nor does it catch any mistakes.
The following code is a script that is run by doing “node nameofScript”.
const { SiteClient, buildModularBlock } = require('datocms-client');
const datoApiToken = "YourAllAccessTokenGoesHere";
const client = new SiteClient(datoApiToken, { environment: 'YourPrimaryEnvHere' });
const pageIdToCopyTo = '51185123'; // This id can be gotten directly from DATO UI
const createFromExistingBlock = async () => {
// Get block to copy
const blockId = '51190123'; // This ID can only be gotten via GRAPHQL with originalId field
const originalBlock = await client.items.find(blockId);
// remove unneeded fields (will be given by dato when block is created)
delete originalBlock["meta"];
delete originalBlock["id"];
delete originalBlock["updatedAt"];
delete originalBlock["createdAt"];
try {
return buildModularBlock(originalBlock);
} catch (err) {
console.log(err)
}
};
const updatePage = async() => {
const pageToCopyTo = await client.items.find(pageIdToCopyTo); // Needed NOT to wipe original blocks
const duplicatedBlock = await createFromExistingBlock();
// Obviously here the script would need to know what locale they need to add the duplicated block to
pageToCopyTo.content["de-AT"].push(duplicatedBlock);
client.items
.update(pageIdToCopyTo, {
content: pageToCopyTo.content
})
};
updatePage();
1 Like