Skip to content
Snippets Groups Projects
Commit 97bc5ed7 authored by Igor Biki's avatar Igor Biki
Browse files

ISTWCMS-7275: Refactor batch process for updating block titles.

Simplified and standardized function names and operations handling for batch processing. Improved context tracking and modularity, and added detailed documentation for better maintainability.
parent c6515cd5
No related branches found
No related tags found
No related merge requests found
...@@ -8,66 +8,81 @@ ...@@ -8,66 +8,81 @@
/** /**
* Updating the title for "references" layout builder blocks titles. * Updating the title for "references" layout builder blocks titles.
*/ */
function uw_custom_blocks_post_update_references_block_titles_updates(&$sandbox) { function uw_custom_blocks_post_update_references_block_titles_updates_12(&$sandbox) {
// Load all nodes. Search for one block (publications reference search), and
// update the title is the title is default. Also use setSyncing(TRUE).
// Repeat the same for the revisions. Make sure to set setSyncing(TRUE)
// not to create any additional revisions.
// Repeat the same for temporary storage.
// Has to use batching, for all!!!! Maybe splitting it to 3 (node, rev, temp).
// Extrapolate common functionality into a separate function to process
// sections/components.
// $storage = \Drupal::entityTypeManager()->getStorage('node');
// $query = $storage->getQuery();
$query = \Drupal::entityQuery('node'); $query = \Drupal::entityQuery('node');
$all_nodes = $query->accessCheck(FALSE)->sort('nid', 'DESC')->execute(); $all_nodes = $query->accessCheck(FALSE)->sort('nid')->execute();
$count = count($all_nodes); $count = count($all_nodes);
$chunks = array_chunk($all_nodes, 1); $chunks = array_chunk($all_nodes, 1);
// Load all revisions for node.
$operations[] = [ $operations[] = [
'_uw_custom_blocks_post_update_references_block_titles_updates_batch_start', '_references_block_titles_updates_batch_start',
$count, [$count],
]; ];
foreach ($chunks as $chunk) { foreach ($chunks as $chunk) {
$operations[] = [ $operations[] = [
'_uw_custom_blocks_post_update_references_block_titles_updates_operation', '_references_block_titles_updates_batch_update',
$chunk, $chunk,
]; ];
} }
$operations[] = [ $operations[] = [
'_uw_custom_blocks_post_update_references_block_titles_updates_batch_temp_storage', '_references_block_titles_updates_batch_temp_storage',
[],
];
$operations[] = [
'_references_block_titles_updates_batch_end',
[],
]; ];
$batch = [ $batch = [
'title' => t('Updating block titles.'), 'title' => t('Updating block titles.'),
'progressive' => TRUE, 'progressive' => TRUE,
'operations' => $operations, 'operations' => $operations,
'finished' => '_uw_custom_blocks_post_update_references_block_titles_updates_finished', 'finished' => '_references_block_titles_updates_batch_finished',
]; ];
batch_set($batch); batch_set($batch);
} }
function _uw_custom_blocks_post_update_references_block_titles_updates_batch_start($count, &$context): void { /**
$context['results']['total'] = $count; * Initializes the batch process context for nodes and revisions updates.
$context['results']['processed'] = 0; *
$context['results']['updated'] = 0; * @param int $count
* The total number of nodes to process.
* @param array $context
* The batch context array, which is updated with initial counts and a status
* message for the batch process.
*/
function _references_block_titles_updates_batch_start($count, &$context): void {
$context['results']['nodes_count'] = $count;
$context['results']['nodes_processed'] = 0;
$context['results']['nodes_updated'] = 0;
$context['results']['revisions_processed'] = 0;
$context['results']['revisions_updated'] = 0;
$context['message'] = t("Processing @count nodes and each node revisions.", ['@count' => $count]);
} }
function _uw_custom_blocks_post_update_references_block_titles_updates_operation($nid, &$context): void { /**
if (!isset($context['results']['processed'])) { * Processes a single node and its revisions to update layout components.
$context['results']['processed'] = 0; *
} * This function updates the label of specific layout components in both the
* node and its revisions. If changes are made, the node or revision is marked
* for saving, and the appropriate counters in the batch context are updated.
*
* @param int $nid
* The node ID of the entity to be processed.
* @param array $context
* A reference to the batch context array that tracks progress and statistics,
* including the number of nodes and revisions processed and updated.
*
* @return void
* This function does not return a value. It modifies the $context array
* directly and updates the node and its revisions in-place.
*/
function _references_block_titles_updates_batch_update($nid, &$context): void {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid); $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
if ($node) { if ($node) {
...@@ -93,14 +108,20 @@ function _uw_custom_blocks_post_update_references_block_titles_updates_operation ...@@ -93,14 +108,20 @@ function _uw_custom_blocks_post_update_references_block_titles_updates_operation
} }
if ($save_needed) { if ($save_needed) {
$node->save(); // $node->save();
$context['results']['updated']++; $context['results']['nodes_updated']++;
} }
$revisions = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);; $context['results']['nodes_processed']++;
$revisions = \Drupal::entityTypeManager()
->getStorage('node')
->revisionIds($node);
foreach ($revisions as $revision) { foreach ($revisions as $revision) {
$revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision); $revision = \Drupal::entityTypeManager()
->getStorage('node')
->loadRevision($revision);
if ($revision) { if ($revision) {
$revision->setSyncing(TRUE); $revision->setSyncing(TRUE);
...@@ -125,25 +146,59 @@ function _uw_custom_blocks_post_update_references_block_titles_updates_operation ...@@ -125,25 +146,59 @@ function _uw_custom_blocks_post_update_references_block_titles_updates_operation
} }
if ($revision_save_needed) { if ($revision_save_needed) {
$revision->save(); // $revision->save();
$context['results']['updated']++; $context['results']['revisions_updated']++;
} }
$context['results']['processed']++; $context['results']['revisions_processed']++;
} }
} }
$context['results']['processed']++;
} }
} }
function _uw_custom_blocks_post_update_references_block_titles_updates_batch_temp_storage(&$context) { /**
* Processes temporary storage during the batch operation.
*
* @param array $context
* The batch context array, which is updated with a status message
* indicating the processing of temporary storage.
*/
function _references_block_titles_updates_batch_temp_storage(&$context) {
// Make sure to load temp storage, and loop over sections there. // Make sure to load temp storage, and loop over sections there.
$context['message'] = t('Processing temporary storage.');
} }
function _uw_custom_blocks_post_update_references_block_titles_updates_finished($success, $results) { /**
$dev = 'halt'; * Updates the batch end context messages for nodes and revisions.
*
* @param array $context
* The batch context array, which gets updated with status messages
* about nodes and revisions processed during the batch operation.
*/
function _references_block_titles_updates_batch_end(&$context) {
$context['message'] = t('Updated @nodes_updated out of @nodes_processed (total @total) nodes.', [
'@nodes_updated' => $context['results']['nodes_updated'],
'@nodes_processed' => $context['results']['nodes_processed'],
'@total' => $context['results']['nodes_count'],
]);
$context['message'] = t('Updated @revisions_updated out of @revisions_processed revisions.', [
'@revisions_updated' => $context['results']['revisions_updated'],
'@revisions_processed' => $context['results']['revisions_processed'],
]);
}
/**
* Final callback for the batch process updating block titles.
*
* @param bool $success
* Indicates whether the batch process completed successfully.
* @param array $results
* An array of results collected during the batch processing.
* @param array $operations
* The remaining operations that were not processed, if any.
*
* @return string
* Translated message indicating the completion of the batch process.
*/
function _references_block_titles_updates_batch_finished($success, $results, $operations) {
return t('Finished.');
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment