Skip to content
Snippets Groups Projects

ISTWCMS-7275: Rename "publication" to "reference" in custom block.

Open Igor Biki requested to merge feature/ISTWCMS-7275-ibiki-publications_rename into 1.1.x
1 file
+ 0
224
Compare changes
  • Side-by-side
  • Inline
  • Eliminated the batch process implementation used for updating "references" layout builder block titles. The removed code included multiple batch operations for nodes, revisions, and temporary storage, which are no longer needed. This cleanup simplifies the uw_custom_blocks.post_update.php file and improves maintainability.
@@ -5,230 +5,6 @@
* 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) {
$query = \Drupal::entityQuery('node');
$all_nodes = $query->accessCheck(FALSE)->sort('nid')->execute();
$count = count($all_nodes);
$chunks = array_chunk($all_nodes, 1);
$operations[] = [
'_references_block_titles_updates_batch_start',
[$count],
];
foreach ($chunks as $chunk) {
$operations[] = [
'_references_block_titles_updates_batch_update',
$chunk,
];
}
$operations[] = [
'_references_block_titles_updates_batch_temp_storage',
[],
];
$operations[] = [
'_references_block_titles_updates_batch_end',
[],
];
$batch = [
'title' => t('Updating block titles.'),
'progressive' => TRUE,
'operations' => $operations,
];
batch_set($batch);
}
/**
* Initializes the batch process context for nodes and revisions updates.
*
* @param int $count
* The total number of nodes to process.
* @param array|\DrushBatchContext $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['results']['temp_storage_updated'] = 0;
$context['message'] = t("References: processing @count nodes and each node revisions.", ['@count' => $count]);
}
/**
* Processes a single node and its revisions to update layout components.
*
* 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|DrushBatchContext $context
* A reference to the batch context array that tracks progress and statistics,
* including the number of nodes and revisions processed and updated.
*/
function _references_block_titles_updates_batch_update($nid, &$context): void {
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $node_storage->load($nid);
if ($node) {
$node->setSyncing(TRUE);
$save_needed = FALSE;
$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();
$context['results']['nodes_updated']++;
}
$context['results']['nodes_processed']++;
$revisions = $node_storage->revisionIds($node);
foreach ($revisions as $revision) {
$revision = $node_storage->loadRevision($revision);
if ($revision) {
$revision->setSyncing(TRUE);
$revision_save_needed = FALSE;
$layout_builder = $revision->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);
$revision_save_needed = TRUE;
}
}
}
}
}
if ($revision_save_needed) {
$revision->save();
$context['results']['revisions_updated']++;
}
$context['results']['revisions_processed']++;
}
}
}
}
/**
* Processes temporary storage during the batch operation.
*
* @param array|DrushBatchContext $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.
$context['message'] = t('References: processing temporary storage.');
// The DB connection.
$database = \Drupal::database();
// Query to get the layout builder from nodes.
$query = $database->select('key_value_expire', 'kv');
$query->fields('kv');
$query->condition('kv.collection', '%layout%', 'LIKE');
$result = $query->execute();
// Step through the results and change out any sections.
foreach ($result as $record) {
$save_needed = FALSE;
// Deserialize the value, which has the sections in it.
$value = unserialize($record->value);
// Get the section storage from the serialized data.
$layout = $value->data['section_storage'];
// Get out the sections.
foreach ($layout->getSections() as &$section) {
// Step through each of the components.
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) {
$value->data['section_storage'] = $layout;
$database->update('key_value_expire')
->fields(['value' => serialize($value)])
->condition('name', $record->name)
->execute();
$context['results']['temp_storage_updated']++;
}
}
}
/**
* Updates the batch end context messages for nodes and revisions.
*
* @param array|DrushBatchContext $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('References: successfully updated @nodes_updated/@nodes_processed nodes', [
'@nodes_updated' => $context['results']['nodes_updated'],
'@nodes_processed' => $context['results']['nodes_processed'],
]);
$context['message'] = t('References: successfully updated @revisions_updated/@revisions_processed revisions.', [
'@revisions_updated' => $context['results']['revisions_updated'],
'@revisions_processed' => $context['results']['revisions_processed'],
]);
$context['message'] = t('References: successfully updated @temp_storage_updated temporary storage.', [
'@temp_storage_updated' => $context['results']['temp_storage_updated'],
]);
}
/**
* Updating the title for "references" layout builder blocks titles.
*/
Loading