Version 1
[yaffs-website] / web / core / modules / node / node.admin.inc
1 <?php
2
3 /**
4  * @file
5  * Content administration and module settings user interface.
6  */
7
8 use Drupal\node\NodeInterface;
9
10 /**
11  * Updates all nodes in the passed-in array with the passed-in field values.
12  *
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
15  * work correctly.
16  *
17  * @param array $nodes
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
21  *   field to.
22  * @param string $langcode
23  *   (optional) The language updates should be applied to. If none is specified
24  *   all available languages are processed.
25  * @param bool $load
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.
31  */
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
34   // of nodes.
35   if (count($nodes) > 10) {
36     $batch = [
37       'operations' => [
38         ['_node_mass_update_batch_process', [$nodes, $updates, $langcode, $load, $revisions]]
39       ],
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',
49     ];
50     batch_set($batch);
51   }
52   else {
53     $storage = \Drupal::entityTypeManager()->getStorage('node');
54     if ($load && !$revisions) {
55       $nodes = $storage->loadMultiple($nodes);
56     }
57     foreach ($nodes as $node) {
58       if ($load && $revisions) {
59         $node = $storage->loadRevision($node);
60       }
61       _node_mass_update_helper($node, $updates, $langcode);
62     }
63     drupal_set_message(t('The update has been performed.'));
64   }
65 }
66
67 /**
68  * Updates individual nodes when fewer than 10 are queued.
69  *
70  * @param \Drupal\node\NodeInterface $node
71  *   A node to update.
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.
77  *
78  * @return \Drupal\node\NodeInterface
79  *   An updated node object.
80  *
81  * @see node_mass_update()
82  */
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;
90     }
91   }
92   $node->save();
93   return $node;
94 }
95
96 /**
97  * Implements callback_batch_operation().
98  *
99  * Executes a batch operation for node_mass_update().
100  *
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.
108  * @param bool $load
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.
116  */
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;
122   }
123
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']);
130     if ($load) {
131       $node = $revisions ?
132         $storage->loadRevision($node) : $storage->load($node);
133     }
134     $node = _node_mass_update_helper($node, $updates, $langcode);
135
136     // Store result for post-processing in the finished callback.
137     $context['results'][] = \Drupal::l($node->label(), $node->urlInfo());
138
139     // Update our progress information.
140     $context['sandbox']['progress']++;
141   }
142
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'];
147   }
148 }
149
150 /**
151  * Implements callback_batch_finished().
152  *
153  * Reports the 'finished' status of batch operation for node_mass_update().
154  *
155  * @param bool $success
156  *   A boolean indicating whether the batch mass update operation successfully
157  *   concluded.
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).
162  *
163  * @see _node_mass_update_batch_process()
164  */
165 function _node_mass_update_batch_finished($success, $results, $operations) {
166   if ($success) {
167     drupal_set_message(t('The update has been performed.'));
168   }
169   else {
170     drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
171     $message = \Drupal::translation()->formatPlural(count($results), '1 item successfully processed:', '@count items successfully processed:');
172     $item_list = [
173       '#theme' => 'item_list',
174       '#items' => $results,
175     ];
176     $message .= drupal_render($item_list);
177     drupal_set_message($message);
178   }
179 }