You are here

public function UpdateManagerInstall::submitForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/update/src/Form/UpdateManagerInstall.php \Drupal\update\Form\UpdateManagerInstall::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

core/modules/update/src/Form/UpdateManagerInstall.php, line 149

Class

UpdateManagerInstall
Configure update settings for this site.

Namespace

Drupal\update\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $local_cache = NULL;
  $all_files = $this
    ->getRequest()->files
    ->get('files', []);
  if ($form_state
    ->getValue('project_url')) {
    $local_cache = update_manager_file_get($form_state
      ->getValue('project_url'));
    if (!$local_cache) {
      $this
        ->messenger()
        ->addError($this
        ->t('Unable to retrieve Drupal project from %url.', [
        '%url' => $form_state
          ->getValue('project_url'),
      ]));
      return;
    }
  }
  elseif (!empty($all_files['project_upload'])) {
    $validators = [
      'file_validate_extensions' => [
        $this->archiverManager
          ->getExtensions(),
      ],
    ];
    if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FileSystemInterface::EXISTS_REPLACE))) {

      // Failed to upload the file. file_save_upload() calls
      // \Drupal\Core\Messenger\MessengerInterface::addError() on failure.
      return;
    }
    $local_cache = $finfo
      ->getFileUri();
  }
  $directory = _update_manager_extract_directory();
  try {
    $archive = update_manager_archive_extract($local_cache, $directory);
  } catch (\Exception $e) {
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
    return;
  }
  $files = $archive
    ->listContents();
  if (!$files) {
    $this
      ->messenger()
      ->addError($this
      ->t('Provided archive contains no files.'));
    return;
  }

  // Unfortunately, we can only use the directory name to determine the
  // project name. Some archivers list the first file as the directory (i.e.,
  // MODULE/) and others list an actual file (i.e., MODULE/README.TXT).
  $project = strtok($files[0], '/\\');
  $archive_errors = $this->moduleHandler
    ->invokeAll('verify_update_archive', [
    $project,
    $local_cache,
    $directory,
  ]);
  if (!empty($archive_errors)) {
    $this
      ->messenger()
      ->addError(array_shift($archive_errors));

    // @todo: Fix me in D8: We need a way to set multiple errors on the same
    // form element and have all of them appear!
    if (!empty($archive_errors)) {
      foreach ($archive_errors as $error) {
        $this
          ->messenger()
          ->addError($error);
      }
    }
    return;
  }

  // Make sure the Updater registry is loaded.
  drupal_get_updaters();
  $project_location = $directory . '/' . $project;
  try {
    $updater = Updater::factory($project_location, $this->root);
  } catch (\Exception $e) {
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
    return;
  }
  try {
    $project_title = Updater::getProjectTitle($project_location);
  } catch (\Exception $e) {
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
    return;
  }
  if (!$project_title) {
    $this
      ->messenger()
      ->addError($this
      ->t('Unable to determine %project name.', [
      '%project' => $project,
    ]));
  }
  if ($updater
    ->isInstalled()) {
    $this
      ->messenger()
      ->addError($this
      ->t('%project is already installed.', [
      '%project' => $project_title,
    ]));
    return;
  }
  $project_real_location = \Drupal::service('file_system')
    ->realpath($project_location);
  $arguments = [
    'project' => $project,
    'updater_name' => get_class($updater),
    'local_url' => $project_real_location,
  ];

  // This process is inherently difficult to test therefore use a state flag.
  $test_authorize = FALSE;
  if (drupal_valid_test_ua()) {
    $test_authorize = \Drupal::state()
      ->get('test_uploaders_via_prompt', FALSE);
  }

  // If the owner of the directory we extracted is the same as the owner of
  // our configuration directory (e.g. sites/default) where we're trying to
  // install the code, there's no need to prompt for FTP/SSH credentials.
  // Instead, we instantiate a Drupal\Core\FileTransfer\Local and invoke
  // update_authorize_run_install() directly.
  if (fileowner($project_real_location) == fileowner($this->sitePath) && !$test_authorize) {
    $this->moduleHandler
      ->loadInclude('update', 'inc', 'update.authorize');
    $filetransfer = new Local($this->root, \Drupal::service('file_system'));
    $response = call_user_func_array('update_authorize_run_install', array_merge([
      $filetransfer,
    ], $arguments));
    if ($response instanceof Response) {
      $form_state
        ->setResponse($response);
    }
  }
  else {

    // The page title must be passed here to ensure it is initially used when
    // authorize.php loads for the first time with the FTP/SSH credentials
    // form.
    system_authorized_init('update_authorize_run_install', __DIR__ . '/../../update.authorize.inc', $arguments, $this
      ->t('Update manager'));
    $form_state
      ->setRedirectUrl(system_authorized_get_url());
  }
}