node.admin.inc 5.38 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Content administration and module settings user interface.
6 7
 */

8
use Drupal\Core\Language\Language;
9
use Drupal\node\NodeInterface;
10

11
/**
12
 * Updates all nodes in the passed-in array with the passed-in field values.
13
 *
14 15 16
 * IMPORTANT NOTE: This function is intended to work when called from a form
 * submission handler. Calling it outside of the form submission process may not
 * work correctly.
17 18
 *
 * @param array $nodes
19
 *   Array of node nids or nodes to update.
20
 * @param array $updates
21 22
 *   Array of key/value pairs with node field names and the value to update that
 *   field to.
23 24 25
 * @param string $langcode
 *   (optional) The language updates should be applied to. If none is specified
 *   all available languages are processed.
26 27 28
 * @param bool $load
 *   (optional) TRUE if $nodes contains an array of node IDs to be loaded, FALSE
 *   if it contains fully loaded nodes. Defaults to FALSE.
29
 */
30
function node_mass_update(array $nodes, array $updates, $langcode = NULL, $load = FALSE) {
31 32 33 34 35
  // We use batch processing to prevent timeout when updating a large number
  // of nodes.
  if (count($nodes) > 10) {
    $batch = array(
      'operations' => array(
36
        array('_node_mass_update_batch_process', array($nodes, $updates, $langcode, $load))
37 38 39 40 41 42 43 44 45
      ),
      'finished' => '_node_mass_update_batch_finished',
      'title' => t('Processing'),
      // We use a single multi-pass operation, so the default
      // 'Remaining x of y operations' message will be confusing here.
      'progress_message' => '',
      'error_message' => t('The update has encountered an error.'),
      // The operations do not live in the .module file, so we need to
      // tell the batch engine which file to load before calling them.
46
      'file' => drupal_get_path('module', 'node') . '/node.admin.inc',
47 48 49 50
    );
    batch_set($batch);
  }
  else {
51 52 53 54 55
    if ($load) {
      $nodes = entity_load_multiple('node', $nodes);
    }
    foreach ($nodes as $node) {
      _node_mass_update_helper($node, $updates, $langcode);
56 57 58 59 60 61
    }
    drupal_set_message(t('The update has been performed.'));
  }
}

/**
62 63
 * Updates individual nodes when fewer than 10 are queued.
 *
64 65 66
 * @param \Drupal\node\NodeInterface $node
 *   A node to update.
 * @param array $updates
67
 *   Associative array of updates.
68 69 70
 * @param string $langcode
 *   (optional) The language updates should be applied to. If none is specified
 *   all available languages are processed.
71
 *
72
 * @return \Drupal\node\NodeInterface
73 74
 *   An updated node object.
 *
75
 * @see node_mass_update()
76
 */
77
function _node_mass_update_helper(NodeInterface $node, array $updates, $langcode = NULL) {
78
  $langcodes = isset($langcode) ? array($langcode) : array_keys($node->getTranslationLanguages());
79 80
  // For efficiency manually save the original node before applying any changes.
  $node->original = clone $node;
81 82
  foreach ($langcodes as $langcode) {
    foreach ($updates as $name => $value) {
83
      $node->getTranslation($langcode)->$name = $value;
84
    }
85
  }
86
  $node->save();
87 88 89 90
  return $node;
}

/**
91
 * Executes a batch operation for node_mass_update().
92 93 94 95 96
 *
 * @param array $nodes
 *   An array of node IDs.
 * @param array $updates
 *   Associative array of updates.
97 98 99
 * @param bool $load
 *   TRUE if $nodes contains an array of node IDs to be loaded, FALSE if it
 *   contains fully loaded nodes.
100 101
 * @param array $context
 *   An array of contextual key/values.
102
 */
103
function _node_mass_update_batch_process(array $nodes, array $updates, $load, array &$context) {
104 105 106 107 108 109 110 111 112 113
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($nodes);
    $context['sandbox']['nodes'] = $nodes;
  }

  // Process nodes by groups of 5.
  $count = min(5, count($context['sandbox']['nodes']));
  for ($i = 1; $i <= $count; $i++) {
    // For each nid, load the node, reset the values, and save it.
114 115 116 117 118
    $node = array_shift($context['sandbox']['nodes']);
    if ($load) {
      $node = entity_load('node', $node);
    }
    $node = _node_mass_update_helper($node, $updates);
119 120

    // Store result for post-processing in the finished callback.
121
    $context['results'][] = l($node->label(), 'node/' . $node->id());
122 123 124 125 126 127 128 129 130 131 132 133 134

    // Update our progress information.
    $context['sandbox']['progress']++;
  }

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}

/**
135
 * Reports the 'finished' status of batch operation for node_mass_update().
136 137 138 139 140 141 142 143
 *
 * @param bool $success
 *   A boolean indicating whether the batch mass update operation successfully
 *   concluded.
 * @param int $results
 *   The number of nodes updated via the batch mode process.
 * @param array $operations
 *   An array of function calls (not used in this function).
144 145 146 147 148 149 150 151
 */
function _node_mass_update_batch_finished($success, $results, $operations) {
  if ($success) {
    drupal_set_message(t('The update has been performed.'));
  }
  else {
    drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
    $message = format_plural(count($results), '1 item successfully processed:', '@count items successfully processed:');
152 153 154 155 156
    $item_list = array(
      '#theme' => 'item_list',
      '#items' => $results,
    );
    $message .= drupal_render($item_list);
157 158 159
    drupal_set_message($message);
  }
}