function node_mass_update in Drupal 8
Same name and namespace in other branches
- 6 modules/node/node.admin.inc \node_mass_update()
- 7 modules/node/node.admin.inc \node_mass_update()
- 9 core/modules/node/node.admin.inc \node_mass_update()
Updates all nodes in the passed-in array with the passed-in field values.
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.
Parameters
array $nodes: Array of node nids or nodes to update.
array $updates: Array of key/value pairs with node field names and the value to update that field to.
string $langcode: (optional) The language updates should be applied to. If none is specified all available languages are processed.
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.
bool $revisions: (optional) TRUE if $nodes contains an array of revision IDs instead of node IDs. Defaults to FALSE; will be ignored if $load is FALSE.
2 calls to node_mass_update()
- hook_user_cancel in core/
modules/ user/ user.api.php - Act on user account cancellations.
- node_user_cancel in core/
modules/ node/ node.module - Implements hook_user_cancel().
File
- core/
modules/ node/ node.admin.inc, line 33 - Content administration and module settings user interface.
Code
function node_mass_update(array $nodes, array $updates, $langcode = NULL, $load = FALSE, $revisions = FALSE) {
// We use batch processing to prevent timeout when updating a large number
// of nodes.
if (count($nodes) > 10) {
$batch = [
'operations' => [
[
'_node_mass_update_batch_process',
[
$nodes,
$updates,
$langcode,
$load,
$revisions,
],
],
],
'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.
'file' => drupal_get_path('module', 'node') . '/node.admin.inc',
];
batch_set($batch);
}
else {
$storage = \Drupal::entityTypeManager()
->getStorage('node');
if ($load && !$revisions) {
$nodes = $storage
->loadMultiple($nodes);
}
foreach ($nodes as $node) {
if ($load && $revisions) {
$node = $storage
->loadRevision($node);
}
_node_mass_update_helper($node, $updates, $langcode);
}
\Drupal::messenger()
->addStatus(t('The update has been performed.'));
}
}