Chris@0: 10) { Chris@0: $batch = [ Chris@0: 'operations' => [ Chris@17: ['_node_mass_update_batch_process', [$nodes, $updates, $langcode, $load, $revisions]], Chris@0: ], Chris@0: 'finished' => '_node_mass_update_batch_finished', Chris@0: 'title' => t('Processing'), Chris@0: // We use a single multi-pass operation, so the default Chris@0: // 'Remaining x of y operations' message will be confusing here. Chris@0: 'progress_message' => '', Chris@0: 'error_message' => t('The update has encountered an error.'), Chris@0: // The operations do not live in the .module file, so we need to Chris@0: // tell the batch engine which file to load before calling them. Chris@0: 'file' => drupal_get_path('module', 'node') . '/node.admin.inc', Chris@0: ]; Chris@0: batch_set($batch); Chris@0: } Chris@0: else { Chris@0: $storage = \Drupal::entityTypeManager()->getStorage('node'); Chris@0: if ($load && !$revisions) { Chris@0: $nodes = $storage->loadMultiple($nodes); Chris@0: } Chris@0: foreach ($nodes as $node) { Chris@0: if ($load && $revisions) { Chris@0: $node = $storage->loadRevision($node); Chris@0: } Chris@0: _node_mass_update_helper($node, $updates, $langcode); Chris@0: } Chris@17: \Drupal::messenger()->addStatus(t('The update has been performed.')); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Updates individual nodes when fewer than 10 are queued. Chris@0: * Chris@0: * @param \Drupal\node\NodeInterface $node Chris@0: * A node to update. Chris@0: * @param array $updates Chris@0: * Associative array of updates. Chris@0: * @param string $langcode Chris@0: * (optional) The language updates should be applied to. If none is specified Chris@0: * all available languages are processed. Chris@0: * Chris@0: * @return \Drupal\node\NodeInterface Chris@0: * An updated node object. Chris@0: * Chris@0: * @see node_mass_update() Chris@0: */ Chris@0: function _node_mass_update_helper(NodeInterface $node, array $updates, $langcode = NULL) { Chris@0: $langcodes = isset($langcode) ? [$langcode] : array_keys($node->getTranslationLanguages()); Chris@0: // For efficiency manually save the original node before applying any changes. Chris@0: $node->original = clone $node; Chris@0: foreach ($langcodes as $langcode) { Chris@0: foreach ($updates as $name => $value) { Chris@0: $node->getTranslation($langcode)->$name = $value; Chris@0: } Chris@0: } Chris@0: $node->save(); Chris@0: return $node; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements callback_batch_operation(). Chris@0: * Chris@0: * Executes a batch operation for node_mass_update(). Chris@0: * Chris@0: * @param array $nodes Chris@0: * An array of node IDs. Chris@0: * @param array $updates Chris@0: * Associative array of updates. Chris@0: * @param string $langcode Chris@0: * The language updates should be applied to. If none is specified all Chris@0: * available languages are processed. Chris@0: * @param bool $load Chris@0: * TRUE if $nodes contains an array of node IDs to be loaded, FALSE if it Chris@0: * contains fully loaded nodes. Chris@0: * @param bool $revisions Chris@0: * (optional) TRUE if $nodes contains an array of revision IDs instead of Chris@0: * node IDs. Defaults to FALSE; will be ignored if $load is FALSE. Chris@0: * @param array|\ArrayAccess $context Chris@0: * An array of contextual key/values. Chris@0: */ Chris@0: function _node_mass_update_batch_process(array $nodes, array $updates, $langcode, $load, $revisions, &$context) { Chris@0: if (!isset($context['sandbox']['progress'])) { Chris@0: $context['sandbox']['progress'] = 0; Chris@0: $context['sandbox']['max'] = count($nodes); Chris@0: $context['sandbox']['nodes'] = $nodes; Chris@0: } Chris@0: Chris@0: // Process nodes by groups of 5. Chris@0: $storage = \Drupal::entityTypeManager()->getStorage('node'); Chris@0: $count = min(5, count($context['sandbox']['nodes'])); Chris@0: for ($i = 1; $i <= $count; $i++) { Chris@0: // For each nid, load the node, reset the values, and save it. Chris@0: $node = array_shift($context['sandbox']['nodes']); Chris@0: if ($load) { Chris@0: $node = $revisions ? Chris@0: $storage->loadRevision($node) : $storage->load($node); Chris@0: } Chris@0: $node = _node_mass_update_helper($node, $updates, $langcode); Chris@0: Chris@0: // Store result for post-processing in the finished callback. Chris@18: $context['results'][] = \Drupal::l($node->label(), $node->toUrl()); Chris@0: Chris@0: // Update our progress information. Chris@0: $context['sandbox']['progress']++; Chris@0: } Chris@0: Chris@0: // Inform the batch engine that we are not finished, Chris@0: // and provide an estimation of the completion level we reached. Chris@0: if ($context['sandbox']['progress'] != $context['sandbox']['max']) { Chris@0: $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements callback_batch_finished(). Chris@0: * Chris@0: * Reports the 'finished' status of batch operation for node_mass_update(). Chris@0: * Chris@0: * @param bool $success Chris@0: * A boolean indicating whether the batch mass update operation successfully Chris@0: * concluded. Chris@0: * @param string[] $results Chris@0: * An array of rendered links to nodes updated via the batch mode process. Chris@0: * @param array $operations Chris@0: * An array of function calls (not used in this function). Chris@0: * Chris@0: * @see _node_mass_update_batch_process() Chris@0: */ Chris@0: function _node_mass_update_batch_finished($success, $results, $operations) { Chris@0: if ($success) { Chris@17: \Drupal::messenger()->addStatus(t('The update has been performed.')); Chris@0: } Chris@0: else { Chris@17: \Drupal::messenger()->addError(t('An error occurred and processing did not complete.')); Chris@0: $message = \Drupal::translation()->formatPlural(count($results), '1 item successfully processed:', '@count items successfully processed:'); Chris@0: $item_list = [ Chris@0: '#theme' => 'item_list', Chris@0: '#items' => $results, Chris@0: ]; Chris@0: $message .= \Drupal::service('renderer')->render($item_list); Chris@17: \Drupal::messenger()->addStatus($message); Chris@0: } Chris@0: }