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
+ 104
49
Compare changes
  • Side-by-side
  • Inline
@@ -8,66 +8,81 @@
/**
* Updating the title for "references" layout builder blocks titles.
*/
function uw_custom_blocks_post_update_references_block_titles_updates(&$sandbox) {
// Load all nodes. Search for one block (publications reference search), and
// update the title is the title is default. Also use setSyncing(TRUE).
// Repeat the same for the revisions. Make sure to set setSyncing(TRUE)
// not to create any additional revisions.
// Repeat the same for temporary storage.
// Has to use batching, for all!!!! Maybe splitting it to 3 (node, rev, temp).
// Extrapolate common functionality into a separate function to process
// sections/components.
// $storage = \Drupal::entityTypeManager()->getStorage('node');
// $query = $storage->getQuery();
function uw_custom_blocks_post_update_references_block_titles_updates_12(&$sandbox) {
$query = \Drupal::entityQuery('node');
$all_nodes = $query->accessCheck(FALSE)->sort('nid', 'DESC')->execute();
$all_nodes = $query->accessCheck(FALSE)->sort('nid')->execute();
$count = count($all_nodes);
$chunks = array_chunk($all_nodes, 1);
// Load all revisions for node.
$operations[] = [
'_uw_custom_blocks_post_update_references_block_titles_updates_batch_start',
$count,
'_references_block_titles_updates_batch_start',
[$count],
];
foreach ($chunks as $chunk) {
$operations[] = [
'_uw_custom_blocks_post_update_references_block_titles_updates_operation',
'_references_block_titles_updates_batch_update',
$chunk,
];
}
$operations[] = [
'_uw_custom_blocks_post_update_references_block_titles_updates_batch_temp_storage',
'_references_block_titles_updates_batch_temp_storage',
[],
];
$operations[] = [
'_references_block_titles_updates_batch_end',
[],
];
$batch = [
'title' => t('Updating block titles.'),
'progressive' => TRUE,
'operations' => $operations,
'finished' => '_uw_custom_blocks_post_update_references_block_titles_updates_finished',
'finished' => '_references_block_titles_updates_batch_finished',
];
batch_set($batch);
}
function _uw_custom_blocks_post_update_references_block_titles_updates_batch_start($count, &$context): void {
$context['results']['total'] = $count;
$context['results']['processed'] = 0;
$context['results']['updated'] = 0;
/**
* Initializes the batch process context for nodes and revisions updates.
*
* @param int $count
* The total number of nodes to process.
* @param array $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['message'] = t("Processing @count nodes and each node revisions.", ['@count' => $count]);
}
function _uw_custom_blocks_post_update_references_block_titles_updates_operation($nid, &$context): void {
if (!isset($context['results']['processed'])) {
$context['results']['processed'] = 0;
}
/**
* 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 $context
* A reference to the batch context array that tracks progress and statistics,
* including the number of nodes and revisions processed and updated.
*
* @return void
* This function does not return a value. It modifies the $context array
* directly and updates the node and its revisions in-place.
*/
function _references_block_titles_updates_batch_update($nid, &$context): void {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
if ($node) {
@@ -93,14 +108,20 @@ function _uw_custom_blocks_post_update_references_block_titles_updates_operation
}
if ($save_needed) {
$node->save();
$context['results']['updated']++;
// $node->save();
$context['results']['nodes_updated']++;
}
$revisions = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);;
$context['results']['nodes_processed']++;
$revisions = \Drupal::entityTypeManager()
->getStorage('node')
->revisionIds($node);
foreach ($revisions as $revision) {
$revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision);
$revision = \Drupal::entityTypeManager()
->getStorage('node')
->loadRevision($revision);
if ($revision) {
$revision->setSyncing(TRUE);
@@ -125,25 +146,59 @@ function _uw_custom_blocks_post_update_references_block_titles_updates_operation
}
if ($revision_save_needed) {
$revision->save();
$context['results']['updated']++;
// $revision->save();
$context['results']['revisions_updated']++;
}
$context['results']['processed']++;
$context['results']['revisions_processed']++;
}
}
$context['results']['processed']++;
}
}
function _uw_custom_blocks_post_update_references_block_titles_updates_batch_temp_storage(&$context) {
/**
* Processes temporary storage during the batch operation.
*
* @param array $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('Processing temporary storage.');
}
function _uw_custom_blocks_post_update_references_block_titles_updates_finished($success, $results) {
$dev = 'halt';
/**
* Updates the batch end context messages for nodes and revisions.
*
* @param array $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('Updated @nodes_updated out of @nodes_processed (total @total) nodes.', [
'@nodes_updated' => $context['results']['nodes_updated'],
'@nodes_processed' => $context['results']['nodes_processed'],
'@total' => $context['results']['nodes_count'],
]);
$context['message'] = t('Updated @revisions_updated out of @revisions_processed revisions.', [
'@revisions_updated' => $context['results']['revisions_updated'],
'@revisions_processed' => $context['results']['revisions_processed'],
]);
}
/**
* Final callback for the batch process updating block titles.
*
* @param bool $success
* Indicates whether the batch process completed successfully.
* @param array $results
* An array of results collected during the batch processing.
* @param array $operations
* The remaining operations that were not processed, if any.
*
* @return string
* Translated message indicating the completion of the batch process.
*/
function _references_block_titles_updates_batch_finished($success, $results, $operations) {
return t('Finished.');
}
Loading