function hook_post_update_NAME

Same name in other branches
  1. 9 core/lib/Drupal/Core/Extension/module.api.php \hook_post_update_NAME()
  2. 8.9.x core/lib/Drupal/Core/Extension/module.api.php \hook_post_update_NAME()
  3. 11.x core/lib/Drupal/Core/Extension/module.api.php \hook_post_update_NAME()

Executes an update which is intended to update data, like entities.

These implementations have to be placed in a MODULE.post_update.php file or a THEME.post_update.php file.

These updates are executed after all hook_update_N() implementations. At this stage Drupal is already fully repaired so you can use any API as you wish.

NAME can be arbitrary machine names. In contrast to hook_update_N() the alphanumeric naming of functions in the file is the only thing which ensures the execution order of those functions. If update order is mandatory, you should add numerical prefix to NAME or make it completely numerical.

Drupal also ensures to not execute the same hook_post_update_NAME() function twice.

Batch updates

If running your update all at once could possibly cause PHP to time out, use the $sandbox parameter to indicate that the Batch API should be used for your update. In this case, your update function acts as an implementation of callback_batch_operation(), and $sandbox acts as the batch context parameter. In your function, read the state information from the previous run from $sandbox (or initialize), run a chunk of updates, save the state in $sandbox, and set $sandbox['#finished'] to a value between 0 and 1 to indicate the percent completed, or 1 if it is finished (you need to do this explicitly in each pass).

See the Batch operations topic for more information on how to use the Batch API.

Parameters

array $sandbox: Stores information for batch updates. See above for more information.

Return value

string|null Optionally, hook_post_update_NAME() hooks may return a translated string that will be displayed to the user after the update has completed. If no message is returned, no message will be presented to the user.

Throws

\Drupal\Core\Utility\UpdateException|PDOException In case of error, update hooks should throw an instance of \Drupal\Core\Utility\UpdateException with a meaningful message for the user. If a database query fails for whatever reason, it will throw a PDOException.

See also

hook_update_N()

hook_removed_post_updates()

Related topics

78 functions implement hook_post_update_NAME()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

big_pipe_post_update_html5_placeholders in core/modules/big_pipe/big_pipe.post_update.php
Clear the render cache.
block_content_post_update_block_library_view_permission in core/modules/block_content/block_content.post_update.php
Update block_content 'block library' view permission.
block_content_post_update_entity_changed_constraint in core/modules/block_content/block_content.post_update.php
Clear the entity type cache.
block_content_post_update_move_custom_block_library in core/modules/block_content/block_content.post_update.php
Moves the custom block library to Content.
block_content_post_update_revision_type in core/modules/block_content/block_content.post_update.php
Update configuration for revision type.

... See full list

File

core/lib/Drupal/Core/Extension/module.api.php, line 872

Code

function hook_post_update_NAME(&$sandbox) {
    // Example of updating some content.
    $node = \Drupal\node\Entity\Node::load(123);
    $node->setTitle('foo');
    $node->save();
    $result = t('Node %nid saved', [
        '%nid' => $node->id(),
    ]);
    // Example of updating some config.
    if (\Drupal::moduleHandler()->moduleExists('taxonomy')) {
        // Update the dependencies of all Vocabulary configuration entities.
        \Drupal::classResolver(\Drupal\Core\Config\Entity\ConfigEntityUpdater::class)->update($sandbox, 'taxonomy_vocabulary');
    }
    return $result;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.