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
+ 249
0
Compare changes
  • Side-by-side
  • Inline
@@ -228,3 +228,252 @@ function _references_block_titles_updates_batch_end(&$context) {
'@temp_storage_updated' => $context['results']['temp_storage_updated'],
]);
}
/**
* Updating the title for "references" layout builder blocks titles.
*/
function uw_custom_blocks_post_update_publications_search_block_rename(&$sandbox) {
$database = \Drupal::database();
$nodes = $database->select('node__layout_builder__layout', 'n')
->fields('n', ['entity_id', 'revision_id', 'delta'])
->condition('n.layout_builder__layout_section', '%uw_cbl_publication_search%', 'like')
->condition('n.layout_builder__layout_section', '%Publication reference search%', 'like')
->execute()
->fetchAllAssoc('entity_id');
$nodes = array_keys($nodes);
$revisions = $database->select('node_revision__layout_builder__layout', 'nr')
->fields('nr', ['entity_id', 'revision_id', 'delta'])
->condition('nr.layout_builder__layout_section', '%uw_cbl_publication_search%', 'like')
->condition('nr.layout_builder__layout_section', '%Publication reference search%', 'like')
->execute()
->fetchAllAssoc('revision_id');
$revisions = array_keys($revisions);
$temp_nodes = $database->select('key_value_expire', 'kv')
->fields('kv', ['collection', 'name'])
->condition('kv.collection', '%layout%', 'like')
->condition('kv.value', '%uw_cbl_publication_search%', 'like')
->condition('kv.value', '%Publication reference search%', 'like')
->execute()
->fetchAllAssoc('name');
$temp_nodes = array_keys($temp_nodes);
$operations[] = [
'_publications_search_block_rename_batch_start',
[count($nodes), count($revisions), count($temp_nodes)],
];
foreach (array_chunk($nodes, 10) as $node_chunk) {
$operations[] = [
'_publications_search_block_rename_batch_node_update',
[$node_chunk],
];
}
foreach (array_chunk($revisions, 10) as $rev_chunk) {
$operations[] = [
'_publications_search_block_rename_batch_revision_update',
[$rev_chunk],
];
}
if (count($temp_nodes)) {
$operations[] = [
'_publications_search_block_rename_batch_temp_storage_update',
[$temp_nodes],
];
}
$operations[] = [
'_publications_search_block_rename_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 $node_count
* The total number of nodes to process.
* @param int $revision_count
* The total number of revisions to process.
* @param int $temp_count
* The total number of temporary storage overrides 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 _publications_search_block_rename_batch_start($node_count, $revision_count, $temp_count, &$context) {
$context['results']['nodes_count'] = $node_count;
$context['results']['revisions_count'] = $revision_count;
$context['results']['temp_count'] = $temp_count;
$context['results']['nodes_updated'] = 0;
$context['results']['revisions_updated'] = 0;
$context['results']['temp_storage_updated'] = 0;
$context['message'] = t("References: processing @count nodes, @rev revisions and @temp temp storages.", [
'@count' => $node_count,
'@rev' => $revision_count,
'@temp' => $temp_count,
]);
}
/**
* The node update logic.
*
* @param array $node_ids
* Array of node ids to process.
* @param array|\DrushBatchContext $context
* The batch context array, which is updated with counts.
*/
function _publications_search_block_rename_batch_node_update($node_ids, &$context) {
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$nodes = $node_storage->loadMultiple($node_ids);
foreach ($nodes as $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']++;
}
}
}
/**
* The revision update logic.
*
* @param array $revision_ids
* Array of revision ids to process.
* @param array|\DrushBatchContext $context
* The batch context array, which is updated with counts.
*/
function _publications_search_block_rename_batch_revision_update($revision_ids, &$context) {
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
foreach ($revision_ids as $revision_id) {
$revision = $node_storage->loadRevision($revision_id);
if ($revision) {
$revision->setSyncing(TRUE);
$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);
$save_needed = TRUE;
}
}
}
}
}
if ($save_needed) {
$revision->save();
$context['results']['revisions_updated']++;
}
}
}
}
/**
* The temporary storage override update logic.
*
* @param array $keys
* Array of name for temporary storage keys.
* @param array|\DrushBatchContext $context
* The batch context array, which is updated with counts.
*/
function _publications_search_block_rename_batch_temp_storage_update($keys, &$context) {
/** @var \Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory $key_value */
$key_value = \Drupal::service('keyvalue.expirable.database');
/** @var \Drupal\Core\KeyValueStore\DatabaseStorageExpirable $overrides */
$overrides = $key_value->get('tempstore.shared.layout_builder.section_storage.overrides');
foreach ($keys as $key) {
$save_needed = FALSE;
$value = $overrides->get($key);
if ($value && isset($value->data['section_storage'])) {
$layout = $value->data['section_storage'];
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;
$overrides->set($key, $value);
$context['results']['temp_storage_updated']++;
}
}
}
/**
* The batch ending process to display messages.
*
* @param array|\DrushBatchContext $context
* The batch context array, which is updated with counts.
*/
function _publications_search_block_rename_batch_end(&$context) {
$context['message'] = t('References: successfully updated @nodes_updated node(s), @revisions_updated revision(s) and @temp_storage_updated temp storage override(s).', [
'@nodes_updated' => $context['results']['nodes_updated'],
'@revisions_updated' => $context['results']['revisions_updated'],
'@temp_storage_updated' => $context['results']['temp_storage_updated'],
]);
}
Loading