diff --git a/src/Plugin/Block/UwCblPublicationSearch.php b/src/Plugin/Block/UwCblReferencesSearch.php similarity index 98% rename from src/Plugin/Block/UwCblPublicationSearch.php rename to src/Plugin/Block/UwCblReferencesSearch.php index 421b2d730de3f3f3be123b76875517bd32aea294..c62a99e59e86dbd35588f0ccbcba998f776f337e 100644 --- a/src/Plugin/Block/UwCblPublicationSearch.php +++ b/src/Plugin/Block/UwCblReferencesSearch.php @@ -11,14 +11,14 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Custom block publication search. + * Custom block reference search. * * @Block( * id = "uw_cbl_publication_search", - * admin_label = @Translation("Publication reference search"), + * admin_label = @Translation("Reference search"), * ) */ -class UwCblPublicationSearch extends BlockBase implements ContainerFactoryPluginInterface { +class UwCblReferencesSearch extends BlockBase implements ContainerFactoryPluginInterface { /** * The entity type manager. @@ -102,14 +102,14 @@ class UwCblPublicationSearch extends BlockBase implements ContainerFactoryPlugin // Get the reference search form using the options selected. $ref_search['form'] = $this->formBuilder->getForm( - 'Drupal\uw_bibcite_reference\Form\PublicationSearchForm', + 'Drupal\uw_bibcite_reference\Form\ReferenceSearchForm', $this->configuration, ); // If there is no block title, then set the label so that // the template will know to place a visually hidden one. if (!$this->configuration['label_display']) { - $ref_search['label'] = $this->t('Publication reference search'); + $ref_search['label'] = $this->t('Reference search'); } return [ diff --git a/src/Plugin/Block/UwCblTeaser.php b/src/Plugin/Block/UwCblTeaser.php index bd1441f263d3a8e5b810453b25005881a5c21f1e..9d660ca2205a890e2fbbede3f6e5397108871706 100644 --- a/src/Plugin/Block/UwCblTeaser.php +++ b/src/Plugin/Block/UwCblTeaser.php @@ -305,7 +305,7 @@ class UwCblTeaser extends BlockBase implements ContainerFactoryPluginInterface { ]; } // Override settings for bibcite references. - elseif ($contentType === 'publication_reference') { + elseif ($contentType === 'reference') { $form[$contentType]['nid'] = [ '#type' => 'entity_autocomplete', '#target_type' => 'bibcite_reference', @@ -378,7 +378,7 @@ class UwCblTeaser extends BlockBase implements ContainerFactoryPluginInterface { } // If bibcite reference is used, make sure to load bibcite entity. - if ($this->configuration['content_type'] === 'publication_reference') { + if ($this->configuration['content_type'] === 'reference') { return $this->entityTypeManager->getStorage('bibcite_reference')->load($entity_id); } @@ -408,7 +408,7 @@ class UwCblTeaser extends BlockBase implements ContainerFactoryPluginInterface { // Set the config for teaser. $this->configuration['content_type'] = $content_type; - if ($content_type == 'publication_reference') { + if ($content_type == 'reference') { $this->configuration['nid'] = $values[$content_type]['nid']; } else { @@ -479,7 +479,7 @@ class UwCblTeaser extends BlockBase implements ContainerFactoryPluginInterface { 'opportunity' => $this->t('Opportunity'), 'profile' => $this->t('Profile'), 'project' => $this->t('Project'), - 'publication_reference' => $this->t('Publication reference'), + 'reference' => $this->t('Reference'), 'service' => $this->t('Service'), ]; } @@ -502,7 +502,7 @@ class UwCblTeaser extends BlockBase implements ContainerFactoryPluginInterface { 'opportunity' => 'uw_ct_opportunity', 'profile' => 'uw_ct_profile', 'project' => 'uw_ct_project', - 'publication_reference' => 'bibcite_reference', + 'reference' => 'bibcite_reference', 'service' => 'uw_ct_service', ]; } diff --git a/src/Service/UwBlockAutoElements.php b/src/Service/UwBlockAutoElements.php index 53a22063b256382a6c7d3519447588a2fbfe903e..ec4a95edc1c2cbe9267ef001369747ea39f0f988 100644 --- a/src/Service/UwBlockAutoElements.php +++ b/src/Service/UwBlockAutoElements.php @@ -694,7 +694,7 @@ class UwBlockAutoElements { } /** - * Function to get the publication reference form elements. + * Function to get the reference form elements. * * @param array $form * The form. @@ -706,10 +706,10 @@ class UwBlockAutoElements { array $config ): void { - // The publication reference container for element. + // The reference container for element. $form['publication_reference'] = [ '#type' => 'fieldset', - '#title' => $this->t('Publication reference list settings'), + '#title' => $this->t('Reference list settings'), '#states' => [ 'visible' => [ 'select[name="settings[content_type]"]' => [ @@ -719,7 +719,7 @@ class UwBlockAutoElements { ], ]; - // The number of publications to show form element. + // The number of references to show a form element. $form['publication_reference']['limit'] = $this->uwBlockService->getLimitPerBlockFormElement( 'publication_reference', $config diff --git a/src/Service/UwBlockAutoGetConfig.php b/src/Service/UwBlockAutoGetConfig.php index 7e7fa82a174ff57a0b58b2dad874db2b0399f165..0bee6d048080ce4c532bf3a459623b2a4225211e 100644 --- a/src/Service/UwBlockAutoGetConfig.php +++ b/src/Service/UwBlockAutoGetConfig.php @@ -294,7 +294,7 @@ class UwBlockAutoGetConfig { } /** - * Get the config for publication references. + * Get the config for references. * * @param array $values * The array for form state values. diff --git a/src/Service/UwBlockAutoRender.php b/src/Service/UwBlockAutoRender.php index 52dfd42c9635f6475650664a43cd2ffd7b7f6e5f..4c6fbcf923537ae93fa4a9b12f103de1a29609df 100644 --- a/src/Service/UwBlockAutoRender.php +++ b/src/Service/UwBlockAutoRender.php @@ -672,13 +672,13 @@ class UwBlockAutoRender { } /** - * Function to get render array for publication references. + * Function to get a render array for references. * * @param array $config * The block config. * * @return array - * Render array for profiles. + * Render an array for profiles. */ private function getPublicationReferenceRenderArray(array $config): array { diff --git a/src/Service/UwBlockService.php b/src/Service/UwBlockService.php index 9fbb16562a7b66e02574aa176e40595a012fbdc5..94eb5b0c87b5fe8a4ac7f43966c883ae6cd070c8 100644 --- a/src/Service/UwBlockService.php +++ b/src/Service/UwBlockService.php @@ -798,7 +798,7 @@ class UwBlockService { 'opportunity' => $this->t('Opportunities'), 'profile' => $this->t('Profiles'), 'project' => $this->t('Projects'), - 'publication_reference' => $this->t('Publication references'), + 'publication_reference' => $this->t('References'), 'service' => $this->t('Services'), ]; } diff --git a/uw_custom_blocks.post_update.php b/uw_custom_blocks.post_update.php new file mode 100644 index 0000000000000000000000000000000000000000..0bd2bb62ea1f57d494919b664699803463b6b4e0 --- /dev/null +++ b/uw_custom_blocks.post_update.php @@ -0,0 +1,286 @@ +<?php + +/** + * @file + * Post update hooks for uw_custom_blocks. + */ + +/** + * Updating the title for "references" layout builder blocks titles. + */ +function uw_custom_blocks_post_update_publications_search_block_rename(&$sandbox) { + $nodes = _publications_search_block_rename_node_prep(); + $revisions = _publications_search_block_rename_revision_prep(); + $temp_nodes = _publications_search_block_rename_temp_storage_prep(); + + $node_count = count(array_keys($nodes)); + $revision_count = count($revisions); + $temp_count = count($temp_nodes); + + // Check if node_count is greater than zero, which means some revisions + // might be duplicated (node with the latest revision is included). + // Those need to be excluded in order not to be processed twice. + if ($revision_count > 0 && $node_count > 0) { + $node_revisions_to_exclude = array_column($nodes, 'revision_id'); + $revisions = array_diff($revisions, $node_revisions_to_exclude); + $revision_count = count($revisions); + } + + if ($node_count == 0 && $revision_count == 0 && $temp_count == 0) { + return; + } + + $operations[] = [ + '_publications_search_block_rename_batch_start', + [$node_count, $revision_count, $temp_count], + ]; + + if ($node_count > 0) { + foreach (array_chunk(array_keys($nodes), 10) as $node_chunk) { + $operations[] = [ + '_publications_search_block_rename_batch_node_update', + [$node_chunk], + ]; + } + } + + if ($revision_count > 0) { + foreach (array_chunk($revisions, 10) as $rev_chunk) { + $operations[] = [ + '_publications_search_block_rename_batch_revision_update', + [$rev_chunk], + ]; + } + } + + if ($temp_count > 0) { + $operations[] = [ + '_publications_search_block_rename_batch_temp_storage_update', + [$temp_nodes], + ]; + } + + $operations[] = [ + '_publications_search_block_rename_batch_end', + [], + ]; + + $batch = [ + 'title' => t('Updating "Reference" layout builder block titles.'), + 'progressive' => TRUE, + 'operations' => $operations, + ]; + + batch_set($batch); +} + +/** + * Prepare a list of nodes with layout builder block. + * + * @return array + * Returns array with node_id => revision_id pairs. + */ +function _publications_search_block_rename_node_prep(): array { + $database = \Drupal::database(); + + return $database->select('node__layout_builder__layout', 'n') + ->fields('n', ['entity_id', 'revision_id']) + ->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'); +} + +/** + * Prepare a list of revisions with layout builder block. + * + * @return array + * Array of revisions. + */ +function _publications_search_block_rename_revision_prep(): array { + $database = \Drupal::database(); + + $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'); + + return array_keys($revisions); +} + +/** + * Prepare a list of temp storage overrides with layout builder block. + */ +function _publications_search_block_rename_temp_storage_prep() { + $database = \Drupal::database(); + + $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'); + + return array_keys($temp_nodes); +} + +/** + * 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 node(s), @rev revision(s) and @temp temp storage override(s).", [ + '@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(array $node_ids, &$context) { + $node_storage = \Drupal::entityTypeManager()->getStorage('node'); + + $nodes = $node_storage->loadMultiple($node_ids); + + foreach ($nodes as $node) { + $node->setSyncing(TRUE); + $layout = $node->get('layout_builder__layout'); + $save_needed = _publications_search_block_rename_process_layout($layout); + + 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(array $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); + $layout = $revision->get('layout_builder__layout'); + $save_needed = _publications_search_block_rename_process_layout($layout); + + 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(array $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']; + $save_needed = _publications_search_block_rename_process_layout($layout); + } + + 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'], + ]); +} + +/** + * Processes layout sections to update specific component labels. + * + * @param mixed $layout + * The layout object containing sections and components. + * + * @return bool + * Is the save needed. + */ +function _publications_search_block_rename_process_layout(&$layout): bool { + $save_needed = FALSE; + + if ($layout) { + 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; + } + } + } + } + } + + return $save_needed; +}