function update_authorize_batch_copy_project

Same name and namespace in other branches
  1. 7.x modules/update/update.authorize.inc \update_authorize_batch_copy_project()
  2. 9 core/modules/update/update.authorize.inc \update_authorize_batch_copy_project()
  3. 8.9.x core/modules/update/update.authorize.inc \update_authorize_batch_copy_project()
  4. 10 core/modules/update/update.authorize.inc \update_authorize_batch_copy_project()

Implements callback_batch_operation().

Copies project to its proper place when authorized to do so.

Parameters

string $project: The canonical short name of the project being installed.

string $updater_name: The name of the Drupal\Core\Updater\Updater class to use for installing this project.

string $local_url: The URL to the locally installed temp directory where the project has already been downloaded and extracted into.

\Drupal\Core\FileTransfer\FileTransfer $filetransfer: The FileTransfer object to use for performing this operation.

array $context: Reference to an array used for Batch API storage.

1 string reference to 'update_authorize_batch_copy_project'
update_authorize_run_update in core/modules/update/update.authorize.inc
Updates existing projects when invoked by authorize.php.

File

core/modules/update/update.authorize.inc, line 131

Code

function update_authorize_batch_copy_project($project, $updater_name, $local_url, $filetransfer, &$context) {
    // Initialize some variables in the Batch API $context array.
    if (!isset($context['results']['log'])) {
        $context['results']['log'] = [];
    }
    if (!isset($context['results']['log'][$project])) {
        $context['results']['log'][$project] = [];
    }
    if (!isset($context['results']['tasks'])) {
        $context['results']['tasks'] = [];
    }
    // The batch API uses a session, and since all the arguments are serialized
    // and unserialized between requests, although the FileTransfer object itself
    // will be reconstructed, the connection pointer itself will be lost. However,
    // the FileTransfer object will still have the connection variable, even
    // though the connection itself is now gone. So, although it's ugly, we have
    // to unset the connection variable at this point so that the FileTransfer
    // object will re-initiate the actual connection.
    unset($filetransfer->connection);
    if (!empty($context['results']['log'][$project]['#abort'])) {
        $context['finished'] = 1;
        return;
    }
    $updater = new $updater_name($local_url, \Drupal::getContainer()->get('update.root'));
    try {
        if ($updater->isInstalled()) {
            // This is an update.
            $tasks = $updater->update($filetransfer);
        }
        else {
            $tasks = $updater->install($filetransfer);
        }
    } catch (UpdaterException $e) {
        _update_batch_create_message($context['results']['log'][$project], t('Error adding / updating'), FALSE);
        _update_batch_create_message($context['results']['log'][$project], $e->getMessage(), FALSE);
        $context['results']['log'][$project]['#abort'] = TRUE;
        return;
    }
    _update_batch_create_message($context['results']['log'][$project], t('Added / updated %project_name successfully', [
        '%project_name' => $project,
    ]));
    if (!empty($tasks)) {
        $context['results']['tasks'] += $tasks;
    }
    // This particular operation is now complete, even though the batch might
    // have other operations to perform.
    $context['finished'] = 1;
}

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