function hook_update_N

Same name and namespace in other branches
  1. 7.x modules/system/system.api.php \hook_update_N()
  2. 9 core/lib/Drupal/Core/Extension/module.api.php \hook_update_N()
  3. 8.9.x core/lib/Drupal/Core/Extension/module.api.php \hook_update_N()
  4. 10 core/lib/Drupal/Core/Extension/module.api.php \hook_update_N()

Related topics

145 functions implement hook_update_N()

Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.

block_content_update_last_removed in core/modules/block_content/block_content.install
Implements hook_update_last_removed().
block_post_update_make_weight_integer in core/modules/block/block.post_update.php
Ensures that all block weights are integers.
block_post_update_set_menu_block_depth_to_null_if_zero in core/modules/block/block.post_update.php
Updates the `depth` setting to NULL if it is 0 in any menu blocks.
block_update_last_removed in core/modules/block/block.install
Implements hook_update_last_removed().
comment_update_last_removed in core/modules/comment/comment.install
Implements hook_update_last_removed().

... See full list

File

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

Code

function hook_update_N(&$sandbox) {
  // For non-batch updates, the signature can simply be:
  // function hook_update_N() {
  // Example function body for adding a field to a database table, which does
  // not require a batch operation:
  $spec = [
    'type' => 'varchar',
    'description' => "New Col",
    'length' => 20,
    'not null' => FALSE,
  ];
  $schema = Database::getConnection()->schema();
  $schema->addField('my_table', 'newcol', $spec);
  // Example of what to do if there is an error during your update.
  if ($some_error_condition_met) {
    throw new UpdateException('Something went wrong; here is what you should do.');
  }
  // Example function body for a batch update. In this example, the values in
  // a database field are updated.
  if (!isset($sandbox['progress'])) {
    // This must be the first run. Initialize the sandbox.
    $sandbox['progress'] = 0;
    $sandbox['current_pk'] = 0;
    $sandbox['max'] = Database::getConnection()->query('SELECT COUNT([my_primary_key]) FROM {my_table}')
      ->fetchField();
  }
  // Update in chunks of 20.
  $records = Database::getConnection()->select('my_table', 'm')
    ->fields('m', [
    'my_primary_key',
    'other_field',
  ])
    ->condition('my_primary_key', $sandbox['current_pk'], '>')
    ->range(0, 20)
    ->orderBy('my_primary_key', 'ASC')
    ->execute();
  foreach ($records as $record) {
    // Here, you would make an update something related to this record. In this
    // example, some text is added to the other field.
    Database::getConnection()->update('my_table')
      ->fields([
      'other_field' => $record->other_field . '-suffix',
    ])
      ->condition('my_primary_key', $record->my_primary_key)
      ->execute();
    $sandbox['progress']++;
    $sandbox['current_pk'] = $record->my_primary_key;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  // To display a message to the user when the update is completed, return it.
  // If you do not want to display a completion message, return nothing.
  return t('All foo bars were updated with the new suffix');
}

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