function DbUpdateController::triggerBatch

Same name in other branches
  1. 8.9.x core/modules/system/src/Controller/DbUpdateController.php \Drupal\system\Controller\DbUpdateController::triggerBatch()
  2. 10 core/modules/system/src/Controller/DbUpdateController.php \Drupal\system\Controller\DbUpdateController::triggerBatch()
  3. 11.x core/modules/system/src/Controller/DbUpdateController.php \Drupal\system\Controller\DbUpdateController::triggerBatch()

Starts the database update batch process.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

1 call to DbUpdateController::triggerBatch()
DbUpdateController::handle in core/modules/system/src/Controller/DbUpdateController.php
Returns a database update page.

File

core/modules/system/src/Controller/DbUpdateController.php, line 582

Class

DbUpdateController
Controller routines for database update routes.

Namespace

Drupal\system\Controller

Code

protected function triggerBatch(Request $request) {
    $maintenance_mode = $this->state
        ->get('system.maintenance_mode', FALSE);
    // Store the current maintenance mode status in the session so that it can
    // be restored at the end of the batch.
    $request->getSession()
        ->set('maintenance_mode', $maintenance_mode);
    // During the update, always put the site into maintenance mode so that
    // in-progress schema changes do not affect visiting users.
    if (empty($maintenance_mode)) {
        $this->state
            ->set('system.maintenance_mode', TRUE);
    }
    
    /** @var \Drupal\Core\Batch\BatchBuilder $batch_builder */
    $batch_builder = (new BatchBuilder())->setTitle($this->t('Updating'))
        ->setInitMessage($this->t('Starting updates'))
        ->setErrorMessage($this->t('An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.'))
        ->setFinishCallback([
        DbUpdateController::class,
        'batchFinished',
    ]);
    // Resolve any update dependencies to determine the actual updates that will
    // be run and the order they will be run in.
    $start = $this->getModuleUpdates();
    $updates = update_resolve_dependencies($start);
    // Store the dependencies for each update function in an array which the
    // batch API can pass in to the batch operation each time it is called. (We
    // do not store the entire update dependency array here because it is
    // potentially very large.)
    $dependency_map = [];
    foreach ($updates as $function => $update) {
        $dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : [];
    }
    // Determine updates to be performed.
    foreach ($updates as $function => $update) {
        if ($update['allowed']) {
            // Set the installed version of each module so updates will start at the
            // correct place. (The updates are already sorted, so we can simply base
            // this on the first one we come across in the above foreach loop.)
            if (isset($start[$update['module']])) {
                \Drupal::service('update.update_hook_registry')->setInstalledVersion($update['module'], $update['number'] - 1);
                unset($start[$update['module']]);
            }
            $batch_builder->addOperation('update_do_one', [
                $update['module'],
                $update['number'],
                $dependency_map[$function],
            ]);
        }
    }
    $post_updates = $this->postUpdateRegistry
        ->getPendingUpdateFunctions();
    if ($post_updates) {
        // Now we rebuild all caches and after that execute the hook_post_update()
        // functions.
        $batch_builder->addOperation('drupal_flush_all_caches', []);
        foreach ($post_updates as $function) {
            $batch_builder->addOperation('update_invoke_post_update', [
                $function,
            ]);
        }
    }
    batch_set($batch_builder->toArray());
    // @todo Revisit once https://www.drupal.org/node/2548095 is in.
    return batch_process(Url::fromUri('base://results'), Url::fromUri('base://start'));
}

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