I was wondering what would be the best way to quickly migrate existing resources to use ContentBlocks.
To summarize, we have 100+ resources with a template that previously used MIGX for some of the content, but has now been adapted to use CB instead. In order to ease the migration, I’ve created a Default Rule for this template that loads a Default Template with fields that still use the old MIGX content. This ensures that all the content is still shown on each page. Then later on the user can migrate the MIGX content to the new CB fields for visual editing.
The problem is that I’ve adapted the template for CB. This means that every resource using this template, but not using CB yet, doesn’t look right. Simply opening every resource in the manager, letting CB apply the Default Rule, and saving it again solves this issue. But as you can imagine, this quickly becomes rather tedious with many resources.
Therefore my question is whether it would be possible to automatically set the ‘Use ContentBlocks?’ boolean to true and parse the existing content using the Default Rule for all the resources using this template. Pretty much akin to what the ‘Rebuild Content’ option on the manager page does, but with the additional rules parsing for the migration. I don’t mind if it involves a slightly hacky script to achieve this. Just hoping it is feasible!
couldn’t you use phpMyAdmin to select all the relevant resources and set it to use CB?
After that a hard cache-delete and/or a “Rebuild content” should do the job.
It’s a bit tricky to set JSON properties from phpMyAdmin, but a simple snippet would work:
$corePath = $modx->getOption('contentblocks.core_path', null, $modx->getOption('core_path') . 'components/contentblocks/');
$ContentBlocks = $modx->getService('contentblocks', 'ContentBlocks', $corePath . 'model/contentblocks/');
if (!$ContentBlocks) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not load ContentBlocks service from plugin. Reinstalling ContentBlocks might fix this.');
return;
}
foreach ($modx->getIterator('modResource') as $resource) {
$resource->setProperty('_useContentBlocks', true, 'contentblocks');
$resource->save();
}
However, I’m not 100% sure that a rebuild after that will also place the content into a layout and field automatically. I think that’s done client-side when first loading a resource when CB is enabled.
So that would mean you also need to do that in the snippet.
Grab the properties field for a resource that has been used with CB as an example. Fetch the existing content with $resource->get('content');, and insert that into the structure CB needs. Then use $resource->setProperty again to store that.
I don’t immediately have a full example at hand, but that is possible.