You are here

function hook_post_update_NAME in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 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.

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 order of functions in the file is the only thing which ensures the execution order of those functions.

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

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()

Related topics

13 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.

block_post_update_disable_blocks_with_missing_contexts in core/modules/block/block.post_update.php
Disable all blocks with missing context IDs in block_update_8001().
field_post_update_entity_reference_handler_setting in core/modules/field/field.post_update.php
Fixes the 'handler' setting for entity reference fields.
field_post_update_save_custom_storage_property in core/modules/field/field.post_update.php
Re-save all field storage config objects to add 'custom_storage' property.
module_test_post_update_test in core/modules/system/tests/modules/module_test/module_test.post_update.php
Test post update function.
system_post_update_recalculate_configuration_entity_dependencies in core/modules/system/system.post_update.php
Re-save all configuration entities to recalculate dependencies.

... See full list

File

core/lib/Drupal/Core/Extension/module.api.php, line 701
Hooks related to module and update systems.

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 disabling blocks with missing condition contexts. Note: The
  // block itself is in a state which is valid at that point.
  // @see block_update_8001()
  // @see block_post_update_disable_blocks_with_missing_contexts()
  $block_update_8001 = \Drupal::keyValue('update_backup')
    ->get('block_update_8001', []);
  $block_ids = array_keys($block_update_8001);
  $block_storage = \Drupal::entityManager()
    ->getStorage('block');
  $blocks = $block_storage
    ->loadMultiple($block_ids);

  /** @var $blocks \Drupal\block\BlockInterface[] */
  foreach ($blocks as $block) {

    // This block has had conditions removed due to an inability to resolve
    // contexts in block_update_8001() so disable it.
    // Disable currently enabled blocks.
    if ($block_update_8001[$block
      ->id()]['status']) {
      $block
        ->setStatus(FALSE);
      $block
        ->save();
    }
  }
  return $result;
}