function node_mass_update in Drupal 9
Same name and namespace in other branches
- 8 core/modules/node/node.admin.inc \node_mass_update()
- 6 modules/node/node.admin.inc \node_mass_update()
- 7 modules/node/node.admin.inc \node_mass_update()
- 10 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 34 - 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_builder = (new BatchBuilder())
->setFile(\Drupal::service('extension.list.module')
->getPath('node') . '/node.admin.inc')
->addOperation('_node_mass_update_batch_process', [
$nodes,
$updates,
$langcode,
$load,
$revisions,
])
->setFinishCallback('_node_mass_update_batch_finished')
->setTitle(t('Processing'))
->setErrorMessage(t('The update has encountered an error.'))
->setProgressMessage('');
batch_set($batch_builder
->toArray());
}
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.'));
}
}