I just want to leave here a solution for a problem I had on different sites with contentblocks.
The problem is when I click on Rebuild Content it get stuck in one point of the process but there is not errors of any kind on the hosting or ModX.
The solution is to do the process “manually” using the following code, any ideas or thoughts to improve this are welcome.
Create a Snnippet called:
rebuildContentBlocks
Code:
<?php
// Output a container for progress messages
echo '<div id="contentblocks-rebuild-progress" style="font-family: Arial, sans-serif; padding: 20px; background: #f9f9f9; border: 1px solid #ddd;">';
echo '<h2>ContentBlocks Rebuild Progress</h2>';
echo '<ul id="progress-list" style="list-style: none; padding: 0;">';
echo '</ul>';
echo '</div>';
// Get all resources with ContentBlocks data
$resources = $modx->getCollection('modResource');
$totalResources = count($resources);
$currentResource = 0;
foreach ($resources as $resource) {
$currentResource++;
$resourceName = $resource->get('pagetitle');
$resourceId = $resource->get('id');
// Force save to trigger ContentBlocks rebuild
$resource->save();
// Output progress message
echo "<script>
var progressList = document.getElementById('progress-list');
var newItem = document.createElement('li');
newItem.innerHTML = 'Processing Resource #$currentResource of $totalResources: <strong>$resourceName</strong> (ID: $resourceId)';
progressList.appendChild(newItem);
</script>";
// Flush the output buffer to display progress in real-time
ob_flush();
flush();
}
// Final completion message
echo "<script>
var progressList = document.getElementById('progress-list');
var newItem = document.createElement('li');
newItem.innerHTML = '<strong>Rebuild Complete!</strong> All $totalResources resources have been processed.';
progressList.appendChild(newItem);
</script>";
return '';
Just call the Snippet into a normal resource, that is it.
Enjoy!