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

ISTWCMS-7275: Update references block titles in layout builder via batch

Introduce a post-update hook to update "Publication reference search" block titles to "Reference search" in layout builder. Use a batch process to handle nodes, their revisions, and temporary storage without creating unnecessary revisions. Ensures efficient processing and maintains data integrity.
parent b4a57191
No related branches found
No related tags found
1 merge request!267ISTWCMS-7275: Rename "publication" to "reference" in custom block.
<?php
/**
* @file
* Post update hooks for uw_custom_blocks.
*/
/**
* Updating the title for "references" layout builder blocks titles.
*/
function uw_custom_blocks_post_update_references_block_titles_updates(&$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).
// $storage = \Drupal::entityTypeManager()->getStorage('node');
// $query = $storage->getQuery();
$query = \Drupal::entityQuery('node');
$all_nodes = $query->accessCheck(FALSE)->sort('nid', 'DESC')->execute();
$count = count($all_nodes);
$chunks = array_chunk($all_nodes, 1);
// Load all revisions for node.
$operations[] = [
'_uw_custom_blocks_post_update_references_block_titles_updates_batch_start',
[$count],
];
foreach ($chunks as $chunk) {
$operations[] = [
'_uw_custom_blocks_post_update_references_block_titles_updates_operation',
$chunk,
];
}
$batch = [
'title' => t('Updating block titles.'),
'progressive' => TRUE,
'operations' => $operations,
'finished' => '_uw_custom_blocks_post_update_references_block_titles_updates_finished',
];
batch_set($batch);
}
function _uw_custom_blocks_post_update_references_block_titles_updates_batch_start($count, &$context): void {
$context['results']['total'] = $count;
$context['results']['processed'] = 0;
}
function _uw_custom_blocks_post_update_references_block_titles_updates_operation($nid, &$context): void {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$node->setSyncing(TRUE);
$save_needed = FALSE;
if ($node) {
$layout_builder = $node->get('layout_builder__layout');
if ($layout_builder) {
foreach ($layout_builder->getSections() as &$section) {
foreach ($section?->getComponents() as &$component) {
if ($component->getPluginId() == 'uw_cbl_publication_search') {
$configuration = $component->get('configuration');
if ($configuration['label'] == 'Publication reference search') {
$configuration['label'] = 'Reference search';
$component->set('configuration', $configuration);
$save_needed = TRUE;
}
}
}
}
}
if ($save_needed) {
$node->save();
}
}
if (!isset($context['results']['processed'])) {
$context['results']['processed'] = 0;
}
$context['results']['processed']++;
}
function _uw_custom_blocks_post_update_references_block_titles_updates_finished($success, $results) {
$dev = 'halt';
}
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