5 * Content administration and module settings user interface.
8 use Drupal\node\NodeInterface;
11 * Updates all nodes in the passed-in array with the passed-in field values.
13 * IMPORTANT NOTE: This function is intended to work when called from a form
14 * submission handler. Calling it outside of the form submission process may not
18 * Array of node nids or nodes to update.
19 * @param array $updates
20 * Array of key/value pairs with node field names and the value to update that
22 * @param string $langcode
23 * (optional) The language updates should be applied to. If none is specified
24 * all available languages are processed.
26 * (optional) TRUE if $nodes contains an array of node IDs to be loaded, FALSE
27 * if it contains fully loaded nodes. Defaults to FALSE.
28 * @param bool $revisions
29 * (optional) TRUE if $nodes contains an array of revision IDs instead of
30 * node IDs. Defaults to FALSE; will be ignored if $load is FALSE.
32 function node_mass_update(array $nodes, array $updates, $langcode = NULL, $load = FALSE, $revisions = FALSE) {
33 // We use batch processing to prevent timeout when updating a large number
35 if (count($nodes) > 10) {
38 ['_node_mass_update_batch_process', [$nodes, $updates, $langcode, $load, $revisions]],
40 'finished' => '_node_mass_update_batch_finished',
41 'title' => t('Processing'),
42 // We use a single multi-pass operation, so the default
43 // 'Remaining x of y operations' message will be confusing here.
44 'progress_message' => '',
45 'error_message' => t('The update has encountered an error.'),
46 // The operations do not live in the .module file, so we need to
47 // tell the batch engine which file to load before calling them.
48 'file' => drupal_get_path('module', 'node') . '/node.admin.inc',
53 $storage = \Drupal::entityTypeManager()->getStorage('node');
54 if ($load && !$revisions) {
55 $nodes = $storage->loadMultiple($nodes);
57 foreach ($nodes as $node) {
58 if ($load && $revisions) {
59 $node = $storage->loadRevision($node);
61 _node_mass_update_helper($node, $updates, $langcode);
63 \Drupal::messenger()->addStatus(t('The update has been performed.'));
68 * Updates individual nodes when fewer than 10 are queued.
70 * @param \Drupal\node\NodeInterface $node
72 * @param array $updates
73 * Associative array of updates.
74 * @param string $langcode
75 * (optional) The language updates should be applied to. If none is specified
76 * all available languages are processed.
78 * @return \Drupal\node\NodeInterface
79 * An updated node object.
81 * @see node_mass_update()
83 function _node_mass_update_helper(NodeInterface $node, array $updates, $langcode = NULL) {
84 $langcodes = isset($langcode) ? [$langcode] : array_keys($node->getTranslationLanguages());
85 // For efficiency manually save the original node before applying any changes.
86 $node->original = clone $node;
87 foreach ($langcodes as $langcode) {
88 foreach ($updates as $name => $value) {
89 $node->getTranslation($langcode)->$name = $value;
97 * Implements callback_batch_operation().
99 * Executes a batch operation for node_mass_update().
101 * @param array $nodes
102 * An array of node IDs.
103 * @param array $updates
104 * Associative array of updates.
105 * @param string $langcode
106 * The language updates should be applied to. If none is specified all
107 * available languages are processed.
109 * TRUE if $nodes contains an array of node IDs to be loaded, FALSE if it
110 * contains fully loaded nodes.
111 * @param bool $revisions
112 * (optional) TRUE if $nodes contains an array of revision IDs instead of
113 * node IDs. Defaults to FALSE; will be ignored if $load is FALSE.
114 * @param array|\ArrayAccess $context
115 * An array of contextual key/values.
117 function _node_mass_update_batch_process(array $nodes, array $updates, $langcode, $load, $revisions, &$context) {
118 if (!isset($context['sandbox']['progress'])) {
119 $context['sandbox']['progress'] = 0;
120 $context['sandbox']['max'] = count($nodes);
121 $context['sandbox']['nodes'] = $nodes;
124 // Process nodes by groups of 5.
125 $storage = \Drupal::entityTypeManager()->getStorage('node');
126 $count = min(5, count($context['sandbox']['nodes']));
127 for ($i = 1; $i <= $count; $i++) {
128 // For each nid, load the node, reset the values, and save it.
129 $node = array_shift($context['sandbox']['nodes']);
132 $storage->loadRevision($node) : $storage->load($node);
134 $node = _node_mass_update_helper($node, $updates, $langcode);
136 // Store result for post-processing in the finished callback.
137 $context['results'][] = \Drupal::l($node->label(), $node->urlInfo());
139 // Update our progress information.
140 $context['sandbox']['progress']++;
143 // Inform the batch engine that we are not finished,
144 // and provide an estimation of the completion level we reached.
145 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
146 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
151 * Implements callback_batch_finished().
153 * Reports the 'finished' status of batch operation for node_mass_update().
155 * @param bool $success
156 * A boolean indicating whether the batch mass update operation successfully
158 * @param string[] $results
159 * An array of rendered links to nodes updated via the batch mode process.
160 * @param array $operations
161 * An array of function calls (not used in this function).
163 * @see _node_mass_update_batch_process()
165 function _node_mass_update_batch_finished($success, $results, $operations) {
167 \Drupal::messenger()->addStatus(t('The update has been performed.'));
170 \Drupal::messenger()->addError(t('An error occurred and processing did not complete.'));
171 $message = \Drupal::translation()->formatPlural(count($results), '1 item successfully processed:', '@count items successfully processed:');
173 '#theme' => 'item_list',
174 '#items' => $results,
176 $message .= \Drupal::service('renderer')->render($item_list);
177 \Drupal::messenger()->addStatus($message);