From 19f62046e7aee511a06287dffe69a4e7f152b3a2 Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Tue, 25 Feb 2025 14:21:41 -0500 Subject: [PATCH 1/8] ISTWCMS-7275: Refactor block classes to rename and streamline logic. Renamed block classes and labels for consistency: Keywords, References, and Authors blocks now reflect updated naming conventions. Removed redundant block form and submit methods to simplify code. Adjusted permissions to align with new keyword editing logic. --- ...orsBlock.php => ReferenceAuthorsBlock.php} | 33 ++++------------- ...dsBlock.php => ReferenceKeywordsBlock.php} | 37 +++++-------------- ...ReferenceBlock.php => ReferencesBlock.php} | 33 ++++------------- 3 files changed, 23 insertions(+), 80 deletions(-) rename src/Plugin/Block/{PublicationAuthorsBlock.php => ReferenceAuthorsBlock.php} (82%) rename src/Plugin/Block/{PublicationKeywordsBlock.php => ReferenceKeywordsBlock.php} (79%) rename src/Plugin/Block/{PublicationReferenceBlock.php => ReferencesBlock.php} (82%) diff --git a/src/Plugin/Block/PublicationAuthorsBlock.php b/src/Plugin/Block/ReferenceAuthorsBlock.php similarity index 82% rename from src/Plugin/Block/PublicationAuthorsBlock.php rename to src/Plugin/Block/ReferenceAuthorsBlock.php index 8b45267..b0ed3dd 100644 --- a/src/Plugin/Block/PublicationAuthorsBlock.php +++ b/src/Plugin/Block/ReferenceAuthorsBlock.php @@ -5,35 +5,34 @@ namespace Drupal\uw_dashboard\Plugin\Block; use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a 'Publication reference authors' block. + * Provides a 'References authors' block. * * @Block( * id = "uw_publication_authors_block", - * admin_label = @Translation("Publication reference authors"), + * admin_label = @Translation("Reference authors"), * ) */ -class PublicationAuthorsBlock extends BlockBase implements ContainerFactoryPluginInterface { +class ReferenceAuthorsBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * Entity type manager from the core. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityTypeManager; + protected EntityTypeManagerInterface $entityTypeManager; /** * The current user. * * @var \Drupal\Core\Session\AccountProxyInterface */ - protected $currentUser; + protected AccountProxyInterface $currentUser; /** * {@inheritdoc} @@ -42,7 +41,7 @@ class PublicationAuthorsBlock extends BlockBase implements ContainerFactoryPlugi ContainerInterface $container, array $configuration, $plugin_id, - $plugin_definition + $plugin_definition, ) { return new static( $configuration, @@ -72,7 +71,7 @@ class PublicationAuthorsBlock extends BlockBase implements ContainerFactoryPlugi $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, - AccountProxyInterface $currentUser + AccountProxyInterface $currentUser, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entityTypeManager; @@ -124,24 +123,6 @@ class PublicationAuthorsBlock extends BlockBase implements ContainerFactoryPlugi ]; } - /** - * {@inheritdoc} - */ - public function blockForm($form, FormStateInterface $form_state) { - - // Get the parent form. - $form = parent::blockForm($form, $form_state); - - return $form; - } - - /** - * {@inheritdoc} - */ - public function blockSubmit($form, FormStateInterface $form_state) { - - } - /** * {@inheritdoc} */ diff --git a/src/Plugin/Block/PublicationKeywordsBlock.php b/src/Plugin/Block/ReferenceKeywordsBlock.php similarity index 79% rename from src/Plugin/Block/PublicationKeywordsBlock.php rename to src/Plugin/Block/ReferenceKeywordsBlock.php index 45febb2..351c932 100644 --- a/src/Plugin/Block/PublicationKeywordsBlock.php +++ b/src/Plugin/Block/ReferenceKeywordsBlock.php @@ -5,35 +5,34 @@ namespace Drupal\uw_dashboard\Plugin\Block; use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a 'Publication reference keywords' block. + * Provides a 'Reference keywords' block. * * @Block( * id = "uw_publication_keywords_block", - * admin_label = @Translation("Publication reference keywords"), + * admin_label = @Translation("Reference keywords"), * ) */ -class PublicationKeywordsBlock extends BlockBase implements ContainerFactoryPluginInterface { +class ReferenceKeywordsBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * Entity type manager from the core. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityTypeManager; + protected EntityTypeManagerInterface $entityTypeManager; /** * The current user. * * @var \Drupal\Core\Session\AccountProxyInterface */ - protected $currentUser; + protected AccountProxyInterface $currentUser; /** * {@inheritdoc} @@ -42,7 +41,7 @@ class PublicationKeywordsBlock extends BlockBase implements ContainerFactoryPlug ContainerInterface $container, array $configuration, $plugin_id, - $plugin_definition + $plugin_definition, ) { return new static( $configuration, @@ -72,7 +71,7 @@ class PublicationKeywordsBlock extends BlockBase implements ContainerFactoryPlug $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, - AccountProxyInterface $currentUser + AccountProxyInterface $currentUser, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entityTypeManager; @@ -85,7 +84,7 @@ class PublicationKeywordsBlock extends BlockBase implements ContainerFactoryPlug public function build() { // Add a message if the user does not have access to the block. - if (!$this->currentUser->hasPermission('edit bibcite_contributor')) { + if (!$this->currentUser->hasPermission('edit bibcite_keyword')) { return [ '#markup' => 'You do not have permission to view this block.', ]; @@ -124,29 +123,11 @@ class PublicationKeywordsBlock extends BlockBase implements ContainerFactoryPlug ]; } - /** - * {@inheritdoc} - */ - public function blockForm($form, FormStateInterface $form_state) { - - // Get the parent form. - $form = parent::blockForm($form, $form_state); - - return $form; - } - - /** - * {@inheritdoc} - */ - public function blockSubmit($form, FormStateInterface $form_state) { - - } - /** * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { - return AccessResult::allowedIfHasPermission($account, 'edit bibcite_contributor'); + return AccessResult::allowedIfHasPermission($account, 'edit bibcite_keyword'); } } diff --git a/src/Plugin/Block/PublicationReferenceBlock.php b/src/Plugin/Block/ReferencesBlock.php similarity index 82% rename from src/Plugin/Block/PublicationReferenceBlock.php rename to src/Plugin/Block/ReferencesBlock.php index 9b579cc..a5b85da 100644 --- a/src/Plugin/Block/PublicationReferenceBlock.php +++ b/src/Plugin/Block/ReferencesBlock.php @@ -5,35 +5,34 @@ namespace Drupal\uw_dashboard\Plugin\Block; use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a 'Publication reference' block. + * Provides a 'References' block. * * @Block( * id = "uw_publication_reference_block", - * admin_label = @Translation("Publication references"), + * admin_label = @Translation("References"), * ) */ -class PublicationReferenceBlock extends BlockBase implements ContainerFactoryPluginInterface { +class ReferencesBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * Entity type manager from the core. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityTypeManager; + protected EntityTypeManagerInterface $entityTypeManager; /** * The current user. * * @var \Drupal\Core\Session\AccountProxyInterface */ - protected $currentUser; + protected AccountProxyInterface $currentUser; /** * {@inheritdoc} @@ -42,7 +41,7 @@ class PublicationReferenceBlock extends BlockBase implements ContainerFactoryPlu ContainerInterface $container, array $configuration, $plugin_id, - $plugin_definition + $plugin_definition, ) { return new static( $configuration, @@ -72,7 +71,7 @@ class PublicationReferenceBlock extends BlockBase implements ContainerFactoryPlu $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, - AccountProxyInterface $currentUser + AccountProxyInterface $currentUser, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entityTypeManager; @@ -124,24 +123,6 @@ class PublicationReferenceBlock extends BlockBase implements ContainerFactoryPlu ]; } - /** - * {@inheritdoc} - */ - public function blockForm($form, FormStateInterface $form_state) { - - // Get the parent form. - $form = parent::blockForm($form, $form_state); - - return $form; - } - - /** - * {@inheritdoc} - */ - public function blockSubmit($form, FormStateInterface $form_state) { - - } - /** * {@inheritdoc} */ -- GitLab From 82f5a1a011c565e4a6703384582f2e84154695d0 Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Tue, 25 Feb 2025 14:22:11 -0500 Subject: [PATCH 2/8] ISTWCMS-7275: Update labels and titles in dashboard views for consistency Standardize wording across dashboard views by replacing "publication" with "reference" in labels and titles. Also updated the tag for the keywords view for better categorization. --- config/install/views.view.uw_view_pub_authors.yml | 4 ++-- config/install/views.view.uw_view_pub_keywords.yml | 4 ++-- config/install/views.view.uw_view_pub_reference.yml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config/install/views.view.uw_view_pub_authors.yml b/config/install/views.view.uw_view_pub_authors.yml index 39b3323..eb82a14 100644 --- a/config/install/views.view.uw_view_pub_authors.yml +++ b/config/install/views.view.uw_view_pub_authors.yml @@ -6,7 +6,7 @@ dependencies: - user - views_autocomplete_filters id: uw_view_pub_authors -label: 'Dashboard - publication reference authors' +label: 'Dashboard - Reference authors' module: views description: 'Find and manage contributors.' tag: default @@ -19,7 +19,7 @@ display: display_plugin: default position: 0 display_options: - title: 'Publication authors' + title: 'Reference authors' fields: leading_title: id: leading_title diff --git a/config/install/views.view.uw_view_pub_keywords.yml b/config/install/views.view.uw_view_pub_keywords.yml index 3ee991c..78e010e 100644 --- a/config/install/views.view.uw_view_pub_keywords.yml +++ b/config/install/views.view.uw_view_pub_keywords.yml @@ -6,10 +6,10 @@ dependencies: - user - views_autocomplete_filters id: uw_view_pub_keywords -label: 'Dashboard - publication reference keywords' +label: 'Dashboard - Reference keywords' module: views description: 'Find and manage keywords.' -tag: '' +tag: default base_table: bibcite_keyword base_field: id display: diff --git a/config/install/views.view.uw_view_pub_reference.yml b/config/install/views.view.uw_view_pub_reference.yml index 7be3ee3..82f4e85 100644 --- a/config/install/views.view.uw_view_pub_reference.yml +++ b/config/install/views.view.uw_view_pub_reference.yml @@ -6,7 +6,7 @@ dependencies: - user - views_autocomplete_filters id: uw_view_pub_reference -label: 'Dashboard - publication reference list' +label: 'Dashboard - Reference list' module: views description: 'Find and manage bibliographic references.' tag: default @@ -19,7 +19,7 @@ display: display_plugin: default position: 0 display_options: - title: Publications + title: References fields: bibcite_reference_bulk_form: id: bibcite_reference_bulk_form -- GitLab From 50e81af9754ffffa7f28d2a7d0bc3d0b4c911f41 Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Tue, 25 Feb 2025 14:24:12 -0500 Subject: [PATCH 3/8] ISTWCMS-7275: Rename 'References authors' block to 'Reference authors' This change updates the block label to use singular form for consistency and clarity. No functional changes were made to the block's behavior. --- src/Plugin/Block/ReferenceAuthorsBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/Block/ReferenceAuthorsBlock.php b/src/Plugin/Block/ReferenceAuthorsBlock.php index b0ed3dd..d6184c2 100644 --- a/src/Plugin/Block/ReferenceAuthorsBlock.php +++ b/src/Plugin/Block/ReferenceAuthorsBlock.php @@ -11,7 +11,7 @@ use Drupal\Core\Session\AccountProxyInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a 'References authors' block. + * Provides a 'Reference authors' block. * * @Block( * id = "uw_publication_authors_block", -- GitLab From a6b92858292aab481c9b5beb0f1d8c97997bb2aa Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Tue, 25 Feb 2025 14:26:07 -0500 Subject: [PATCH 4/8] ISTWCMS-7275: Rename ReferencesBlock to ReferenceBlock for consistency. The block class name and annotation were updated to align with a singular naming convention. This change ensures clarity and uniformity across the codebase. --- src/Plugin/Block/{ReferencesBlock.php => ReferenceBlock.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/Plugin/Block/{ReferencesBlock.php => ReferenceBlock.php} (96%) diff --git a/src/Plugin/Block/ReferencesBlock.php b/src/Plugin/Block/ReferenceBlock.php similarity index 96% rename from src/Plugin/Block/ReferencesBlock.php rename to src/Plugin/Block/ReferenceBlock.php index a5b85da..60ccec1 100644 --- a/src/Plugin/Block/ReferencesBlock.php +++ b/src/Plugin/Block/ReferenceBlock.php @@ -11,14 +11,14 @@ use Drupal\Core\Session\AccountProxyInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a 'References' block. + * Provides a 'Reference' block. * * @Block( * id = "uw_publication_reference_block", * admin_label = @Translation("References"), * ) */ -class ReferencesBlock extends BlockBase implements ContainerFactoryPluginInterface { +class ReferenceBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * Entity type manager from the core. -- GitLab From 446a0cf1774f0ceac741474623e198cc6d9b35ed Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Tue, 25 Feb 2025 14:42:35 -0500 Subject: [PATCH 5/8] ISTWCMS-7275: Handle null view scenarios in Reference block plugins Added null-safe operators and fallback checks to prevent errors when views are missing or fail to load. If a view is unavailable, the blocks now return a user-friendly error message instead of triggering null reference exceptions. --- src/Plugin/Block/ReferenceAuthorsBlock.php | 12 ++++++++++-- src/Plugin/Block/ReferenceBlock.php | 12 ++++++++++-- src/Plugin/Block/ReferenceKeywordsBlock.php | 12 ++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Plugin/Block/ReferenceAuthorsBlock.php b/src/Plugin/Block/ReferenceAuthorsBlock.php index d6184c2..92ff4b7 100644 --- a/src/Plugin/Block/ReferenceAuthorsBlock.php +++ b/src/Plugin/Block/ReferenceAuthorsBlock.php @@ -94,8 +94,16 @@ class ReferenceAuthorsBlock extends BlockBase implements ContainerFactoryPluginI /** @var \Drupal\views\ViewExecutable $view */ $view = $this->entityTypeManager ->getStorage('view') - ->load('uw_view_pub_authors') - ->getExecutable(); + ?->load('uw_view_pub_authors') + ?->getExecutable(); + + // In case something failed, like view not found, return an empty array + // before using view, to prevent call function on null errors. + if (!$view) { + return [ + '#markup' => $this->t('There was an error loading the block.') + ]; + } // Set the display. $view->setDisplay('pub_reference_authors'); diff --git a/src/Plugin/Block/ReferenceBlock.php b/src/Plugin/Block/ReferenceBlock.php index 60ccec1..9e9be29 100644 --- a/src/Plugin/Block/ReferenceBlock.php +++ b/src/Plugin/Block/ReferenceBlock.php @@ -94,8 +94,16 @@ class ReferenceBlock extends BlockBase implements ContainerFactoryPluginInterfac /** @var \Drupal\views\ViewExecutable $view */ $view = $this->entityTypeManager ->getStorage('view') - ->load('uw_view_pub_reference') - ->getExecutable(); + ?->load('uw_view_pub_reference') + ?->getExecutable(); + + // In case something failed, like view not found, return an empty array + // before using view, to prevent call function on null errors. + if (!$view) { + return [ + '#markup' => $this->t('There was an error loading the block.') + ]; + } // Set the display. $view->setDisplay('pub_reference_list'); diff --git a/src/Plugin/Block/ReferenceKeywordsBlock.php b/src/Plugin/Block/ReferenceKeywordsBlock.php index 351c932..5a9b793 100644 --- a/src/Plugin/Block/ReferenceKeywordsBlock.php +++ b/src/Plugin/Block/ReferenceKeywordsBlock.php @@ -94,8 +94,16 @@ class ReferenceKeywordsBlock extends BlockBase implements ContainerFactoryPlugin /** @var \Drupal\views\ViewExecutable $view */ $view = $this->entityTypeManager ->getStorage('view') - ->load('uw_view_pub_keywords') - ->getExecutable(); + ?->load('uw_view_pub_keywords') + ?->getExecutable(); + + // In case something failed, like view not found, return an empty array + // before using view, to prevent call function on null errors. + if (!$view) { + return [ + '#markup' => $this->t('There was an error loading the block.') + ]; + } // Set the display. $view->setDisplay('pub_reference_keywords'); -- GitLab From e5e8997dfb1b9e509cd5e43da0f0d6864ede1f33 Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Tue, 25 Feb 2025 15:14:28 -0500 Subject: [PATCH 6/8] ISTWCMS-7275: Fix minor syntax consistency in error message rendering. A trailing comma was added to ensure consistent syntax formatting across multiple block plugins. This change does not impact functionality but improves code readability and adherence to coding standards. --- src/Plugin/Block/ReferenceAuthorsBlock.php | 2 +- src/Plugin/Block/ReferenceBlock.php | 2 +- src/Plugin/Block/ReferenceKeywordsBlock.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Plugin/Block/ReferenceAuthorsBlock.php b/src/Plugin/Block/ReferenceAuthorsBlock.php index 92ff4b7..7ba7b87 100644 --- a/src/Plugin/Block/ReferenceAuthorsBlock.php +++ b/src/Plugin/Block/ReferenceAuthorsBlock.php @@ -101,7 +101,7 @@ class ReferenceAuthorsBlock extends BlockBase implements ContainerFactoryPluginI // before using view, to prevent call function on null errors. if (!$view) { return [ - '#markup' => $this->t('There was an error loading the block.') + '#markup' => $this->t('There was an error loading the block.'), ]; } diff --git a/src/Plugin/Block/ReferenceBlock.php b/src/Plugin/Block/ReferenceBlock.php index 9e9be29..b908ebe 100644 --- a/src/Plugin/Block/ReferenceBlock.php +++ b/src/Plugin/Block/ReferenceBlock.php @@ -101,7 +101,7 @@ class ReferenceBlock extends BlockBase implements ContainerFactoryPluginInterfac // before using view, to prevent call function on null errors. if (!$view) { return [ - '#markup' => $this->t('There was an error loading the block.') + '#markup' => $this->t('There was an error loading the block.'), ]; } diff --git a/src/Plugin/Block/ReferenceKeywordsBlock.php b/src/Plugin/Block/ReferenceKeywordsBlock.php index 5a9b793..8c65458 100644 --- a/src/Plugin/Block/ReferenceKeywordsBlock.php +++ b/src/Plugin/Block/ReferenceKeywordsBlock.php @@ -101,7 +101,7 @@ class ReferenceKeywordsBlock extends BlockBase implements ContainerFactoryPlugin // before using view, to prevent call function on null errors. if (!$view) { return [ - '#markup' => $this->t('There was an error loading the block.') + '#markup' => $this->t('There was an error loading the block.'), ]; } -- GitLab From f9ab9ce900fbba672d978e5a860ec1d93802ac4a Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Wed, 26 Feb 2025 09:22:29 -0500 Subject: [PATCH 7/8] ISTWCMS-7275: Fix incorrect constructor doc comments in block plugins. Updated the constructor doc comments for ReferenceKeywordsBlock, ReferenceBlock, and ReferenceAuthorsBlock to correctly reflect their class names. This ensures clarity and consistency in documentation. --- src/Plugin/Block/ReferenceAuthorsBlock.php | 2 +- src/Plugin/Block/ReferenceBlock.php | 2 +- src/Plugin/Block/ReferenceKeywordsBlock.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Plugin/Block/ReferenceAuthorsBlock.php b/src/Plugin/Block/ReferenceAuthorsBlock.php index 7ba7b87..708839d 100644 --- a/src/Plugin/Block/ReferenceAuthorsBlock.php +++ b/src/Plugin/Block/ReferenceAuthorsBlock.php @@ -53,7 +53,7 @@ class ReferenceAuthorsBlock extends BlockBase implements ContainerFactoryPluginI } /** - * ContentManagementMenuBlock constructor. + * ReferenceAuthorsBlock constructor. * * @param array $configuration * A configuration array containing information about the plugin instance. diff --git a/src/Plugin/Block/ReferenceBlock.php b/src/Plugin/Block/ReferenceBlock.php index b908ebe..706b98c 100644 --- a/src/Plugin/Block/ReferenceBlock.php +++ b/src/Plugin/Block/ReferenceBlock.php @@ -53,7 +53,7 @@ class ReferenceBlock extends BlockBase implements ContainerFactoryPluginInterfac } /** - * ContentManagementMenuBlock constructor. + * ReferenceBlock constructor. * * @param array $configuration * A configuration array containing information about the plugin instance. diff --git a/src/Plugin/Block/ReferenceKeywordsBlock.php b/src/Plugin/Block/ReferenceKeywordsBlock.php index 8c65458..454f2f9 100644 --- a/src/Plugin/Block/ReferenceKeywordsBlock.php +++ b/src/Plugin/Block/ReferenceKeywordsBlock.php @@ -53,7 +53,7 @@ class ReferenceKeywordsBlock extends BlockBase implements ContainerFactoryPlugin } /** - * ContentManagementMenuBlock constructor. + * ReferenceKeywordsBlock constructor. * * @param array $configuration * A configuration array containing information about the plugin instance. -- GitLab From effa986b863532f4239bcb45d58a900ad2ffbf1b Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Thu, 20 Mar 2025 13:45:19 -0400 Subject: [PATCH 8/8] ISTWCMS-7275: Add post-update hook to rename publication block titles This update introduces a post-update hook to rename specific publication block titles within user dashboards for consistency and clarity. It retrieves and updates user data while logging progress and handling potential errors to ensure updates are applied correctly. --- uw_dashboard.post_update.php | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 uw_dashboard.post_update.php diff --git a/uw_dashboard.post_update.php b/uw_dashboard.post_update.php new file mode 100644 index 0000000..ebb8b27 --- /dev/null +++ b/uw_dashboard.post_update.php @@ -0,0 +1,65 @@ +<?php + +/** + * @file + * Post update hooks for uw_dashboard module. + */ + +/** + * Renaming publications blocks titles to reference block titles. + */ +function uw_dashboard_post_update_blocks_rename(&$sandbox) { + $rename = [ + 'uw_publication_authors_block' => ['Publication reference authors' => 'Reference authors'], + 'uw_publication_reference_block' => ['Publication references' => 'References'], + 'uw_publication_keywords_block' => ['Publication reference keywords' => 'Reference keywords'], + 'uw_cbl_publication_search' => ['Publication reference search' => 'Reference search'], + ]; + + $database = \Drupal::database(); + + $query = $database->select('users_data', 'ud') + ->fields('ud', ['uid', 'value']) + ->condition('module', 'dashboards') + ->condition('name', 'my_dashboard'); + + $result = $query->execute()->fetchAllAssoc('uid'); + + \Drupal::logger('uw_dashboard') + ->info('Updating @num dashboards', ['@num' => count($result)]); + + /** @var \Drupal\user\UserDataInterface $dataService */ + $dataService = \Drupal::service('user.data'); + + foreach ($result as $uid => $row) { + try { + $data = unserialize($row->value, ['allowed_classes' => FALSE]); + $save_required = FALSE; + + foreach ($data as &$section) { + foreach ($section['components'] as &$block) { + if (array_key_exists($block['configuration']['id'], $rename)) { + $titles = $rename[$block['configuration']['id']]; + foreach ($titles as $old => $new) { + if (trim($block['configuration']['label']) === $old) { + $block['configuration']['label'] = $new; + $save_required = TRUE; + } + } + } + } + } + + if ($save_required) { + $dataService->set('dashboards', $uid, 'my_dashboard', serialize($data)); + } + } + catch (\Exception $ex) { + \Drupal::logger('uw_dashboard') + ->error('Block rename failed for user: @uid, with message: @message', [ + '@uid' => $uid, + '@message' => $ex->getMessage(), + ]); + } + } +} -- GitLab