annotate core/modules/node/node.admin.inc @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Content administration and module settings user interface.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\node\NodeInterface;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Updates all nodes in the passed-in array with the passed-in field values.
Chris@0 12 *
Chris@0 13 * IMPORTANT NOTE: This function is intended to work when called from a form
Chris@0 14 * submission handler. Calling it outside of the form submission process may not
Chris@0 15 * work correctly.
Chris@0 16 *
Chris@0 17 * @param array $nodes
Chris@0 18 * Array of node nids or nodes to update.
Chris@0 19 * @param array $updates
Chris@0 20 * Array of key/value pairs with node field names and the value to update that
Chris@0 21 * field to.
Chris@0 22 * @param string $langcode
Chris@0 23 * (optional) The language updates should be applied to. If none is specified
Chris@0 24 * all available languages are processed.
Chris@0 25 * @param bool $load
Chris@0 26 * (optional) TRUE if $nodes contains an array of node IDs to be loaded, FALSE
Chris@0 27 * if it contains fully loaded nodes. Defaults to FALSE.
Chris@0 28 * @param bool $revisions
Chris@0 29 * (optional) TRUE if $nodes contains an array of revision IDs instead of
Chris@0 30 * node IDs. Defaults to FALSE; will be ignored if $load is FALSE.
Chris@0 31 */
Chris@0 32 function node_mass_update(array $nodes, array $updates, $langcode = NULL, $load = FALSE, $revisions = FALSE) {
Chris@0 33 // We use batch processing to prevent timeout when updating a large number
Chris@0 34 // of nodes.
Chris@0 35 if (count($nodes) > 10) {
Chris@0 36 $batch = [
Chris@0 37 'operations' => [
Chris@17 38 ['_node_mass_update_batch_process', [$nodes, $updates, $langcode, $load, $revisions]],
Chris@0 39 ],
Chris@0 40 'finished' => '_node_mass_update_batch_finished',
Chris@0 41 'title' => t('Processing'),
Chris@0 42 // We use a single multi-pass operation, so the default
Chris@0 43 // 'Remaining x of y operations' message will be confusing here.
Chris@0 44 'progress_message' => '',
Chris@0 45 'error_message' => t('The update has encountered an error.'),
Chris@0 46 // The operations do not live in the .module file, so we need to
Chris@0 47 // tell the batch engine which file to load before calling them.
Chris@0 48 'file' => drupal_get_path('module', 'node') . '/node.admin.inc',
Chris@0 49 ];
Chris@0 50 batch_set($batch);
Chris@0 51 }
Chris@0 52 else {
Chris@0 53 $storage = \Drupal::entityTypeManager()->getStorage('node');
Chris@0 54 if ($load && !$revisions) {
Chris@0 55 $nodes = $storage->loadMultiple($nodes);
Chris@0 56 }
Chris@0 57 foreach ($nodes as $node) {
Chris@0 58 if ($load && $revisions) {
Chris@0 59 $node = $storage->loadRevision($node);
Chris@0 60 }
Chris@0 61 _node_mass_update_helper($node, $updates, $langcode);
Chris@0 62 }
Chris@17 63 \Drupal::messenger()->addStatus(t('The update has been performed.'));
Chris@0 64 }
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Updates individual nodes when fewer than 10 are queued.
Chris@0 69 *
Chris@0 70 * @param \Drupal\node\NodeInterface $node
Chris@0 71 * A node to update.
Chris@0 72 * @param array $updates
Chris@0 73 * Associative array of updates.
Chris@0 74 * @param string $langcode
Chris@0 75 * (optional) The language updates should be applied to. If none is specified
Chris@0 76 * all available languages are processed.
Chris@0 77 *
Chris@0 78 * @return \Drupal\node\NodeInterface
Chris@0 79 * An updated node object.
Chris@0 80 *
Chris@0 81 * @see node_mass_update()
Chris@0 82 */
Chris@0 83 function _node_mass_update_helper(NodeInterface $node, array $updates, $langcode = NULL) {
Chris@0 84 $langcodes = isset($langcode) ? [$langcode] : array_keys($node->getTranslationLanguages());
Chris@0 85 // For efficiency manually save the original node before applying any changes.
Chris@0 86 $node->original = clone $node;
Chris@0 87 foreach ($langcodes as $langcode) {
Chris@0 88 foreach ($updates as $name => $value) {
Chris@0 89 $node->getTranslation($langcode)->$name = $value;
Chris@0 90 }
Chris@0 91 }
Chris@0 92 $node->save();
Chris@0 93 return $node;
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Implements callback_batch_operation().
Chris@0 98 *
Chris@0 99 * Executes a batch operation for node_mass_update().
Chris@0 100 *
Chris@0 101 * @param array $nodes
Chris@0 102 * An array of node IDs.
Chris@0 103 * @param array $updates
Chris@0 104 * Associative array of updates.
Chris@0 105 * @param string $langcode
Chris@0 106 * The language updates should be applied to. If none is specified all
Chris@0 107 * available languages are processed.
Chris@0 108 * @param bool $load
Chris@0 109 * TRUE if $nodes contains an array of node IDs to be loaded, FALSE if it
Chris@0 110 * contains fully loaded nodes.
Chris@0 111 * @param bool $revisions
Chris@0 112 * (optional) TRUE if $nodes contains an array of revision IDs instead of
Chris@0 113 * node IDs. Defaults to FALSE; will be ignored if $load is FALSE.
Chris@0 114 * @param array|\ArrayAccess $context
Chris@0 115 * An array of contextual key/values.
Chris@0 116 */
Chris@0 117 function _node_mass_update_batch_process(array $nodes, array $updates, $langcode, $load, $revisions, &$context) {
Chris@0 118 if (!isset($context['sandbox']['progress'])) {
Chris@0 119 $context['sandbox']['progress'] = 0;
Chris@0 120 $context['sandbox']['max'] = count($nodes);
Chris@0 121 $context['sandbox']['nodes'] = $nodes;
Chris@0 122 }
Chris@0 123
Chris@0 124 // Process nodes by groups of 5.
Chris@0 125 $storage = \Drupal::entityTypeManager()->getStorage('node');
Chris@0 126 $count = min(5, count($context['sandbox']['nodes']));
Chris@0 127 for ($i = 1; $i <= $count; $i++) {
Chris@0 128 // For each nid, load the node, reset the values, and save it.
Chris@0 129 $node = array_shift($context['sandbox']['nodes']);
Chris@0 130 if ($load) {
Chris@0 131 $node = $revisions ?
Chris@0 132 $storage->loadRevision($node) : $storage->load($node);
Chris@0 133 }
Chris@0 134 $node = _node_mass_update_helper($node, $updates, $langcode);
Chris@0 135
Chris@0 136 // Store result for post-processing in the finished callback.
Chris@18 137 $context['results'][] = \Drupal::l($node->label(), $node->toUrl());
Chris@0 138
Chris@0 139 // Update our progress information.
Chris@0 140 $context['sandbox']['progress']++;
Chris@0 141 }
Chris@0 142
Chris@0 143 // Inform the batch engine that we are not finished,
Chris@0 144 // and provide an estimation of the completion level we reached.
Chris@0 145 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
Chris@0 146 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
Chris@0 147 }
Chris@0 148 }
Chris@0 149
Chris@0 150 /**
Chris@0 151 * Implements callback_batch_finished().
Chris@0 152 *
Chris@0 153 * Reports the 'finished' status of batch operation for node_mass_update().
Chris@0 154 *
Chris@0 155 * @param bool $success
Chris@0 156 * A boolean indicating whether the batch mass update operation successfully
Chris@0 157 * concluded.
Chris@0 158 * @param string[] $results
Chris@0 159 * An array of rendered links to nodes updated via the batch mode process.
Chris@0 160 * @param array $operations
Chris@0 161 * An array of function calls (not used in this function).
Chris@0 162 *
Chris@0 163 * @see _node_mass_update_batch_process()
Chris@0 164 */
Chris@0 165 function _node_mass_update_batch_finished($success, $results, $operations) {
Chris@0 166 if ($success) {
Chris@17 167 \Drupal::messenger()->addStatus(t('The update has been performed.'));
Chris@0 168 }
Chris@0 169 else {
Chris@17 170 \Drupal::messenger()->addError(t('An error occurred and processing did not complete.'));
Chris@0 171 $message = \Drupal::translation()->formatPlural(count($results), '1 item successfully processed:', '@count items successfully processed:');
Chris@0 172 $item_list = [
Chris@0 173 '#theme' => 'item_list',
Chris@0 174 '#items' => $results,
Chris@0 175 ];
Chris@0 176 $message .= \Drupal::service('renderer')->render($item_list);
Chris@17 177 \Drupal::messenger()->addStatus($message);
Chris@0 178 }
Chris@0 179 }