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

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.
parent f9ab9ce9
No related branches found
No related tags found
1 merge request!46ISTWCMS-7275: Refactor block classes to rename and streamline logic.
<?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(),
]);
}
}
}
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