summaryrefslogtreecommitdiff
path: root/modules/node/node.module
diff options
context:
space:
mode:
Diffstat (limited to 'modules/node/node.module')
-rw-r--r--modules/node/node.module95
1 files changed, 95 insertions, 0 deletions
diff --git a/modules/node/node.module b/modules/node/node.module
index 7e1865565..56de974d5 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -2533,3 +2533,98 @@ function node_unpublish_by_keyword_action($node, $context) {
}
}
}
+
+/**
+ * Make mass update of nodes, changing all nodes in the $nodes array
+ * to update them with the field values in $updates.
+ *
+ * IMPORTANT NOTE: This function is intended to work when called
+ * from a form submit handler. Calling it outside of the form submission
+ * process may not work correctly.
+ *
+ * @param array $nodes
+ * Array of node nids to update.
+ * @param array $updates
+ * Array of key/value pairs with node field names and the
+ * value to update that field to.
+ */
+function node_mass_update($nodes, $updates) {
+ // We use batch processing to prevent timeout when updating a large number
+ // of nodes.
+ if (count($nodes) > 10) {
+ $batch = array(
+ 'operations' => array(
+ array('_node_mass_update_batch_process', array($nodes, $updates))
+ ),
+ 'finished' => '_node_mass_update_batch_finished',
+ 'title' => t('Processing'),
+ 'error_message' => t('The update has encountered an error.'),
+ );
+ batch_set($batch);
+ }
+ else {
+ foreach($nodes as $nid) {
+ _node_mass_update_helper($nid, $updates);
+ }
+ drupal_set_message(t('The update has been performed.'));
+ }
+}
+
+/**
+ * Node Mass Update - helper function.
+ */
+function _node_mass_update_helper($nid, $updates) {
+ $node = node_load($nid, NULL, TRUE);
+ foreach ($updates as $name => $value) {
+ $node->$name = $value;
+ }
+ node_save($node);
+ return $node;
+}
+
+/**
+ * Node Mass Update Batch operation
+ */
+function _node_mass_update_batch_process($nodes, $updates, &$context) {
+ 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.
+ $nid = array_shift($context['sandbox']['nodes']);
+ $node = _node_mass_update_helper($nid, $updates);
+
+ // Store result for post-processing in the finished callback.
+ $context['results'][] = l($node->title, 'node/'. $node->nid);
+
+ // Update our progress information.
+ $context['sandbox']['progress']++;
+ $context['message'] = t('Processing %title', array('%title' => $node->title));
+ }
+
+ // 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'];
+ }
+}
+
+/**
+ * Node Mass Update Batch 'finished' callback.
+ */
+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:');
+ $message .= theme('item_list', $results);
+ drupal_set_message($message);
+ }
+} \ No newline at end of file