You are here

public function UpgradeStatusForm::submitForm in Upgrade Status 8.3

Same name and namespace in other branches
  1. 8 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::submitForm()
  2. 8.2 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::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

src/Form/UpgradeStatusForm.php, line 1078

Class

UpgradeStatusForm

Namespace

Drupal\upgrade_status\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Reset extension lists for better Drupal 9 compatibility info.
  $this->projectCollector
    ->resetLists();
  $operations = $list = [];
  $projects = $this->projectCollector
    ->collectProjects();
  $submitted = $form_state
    ->getValues();
  $next_steps = $this->projectCollector
    ->getNextStepInfo();
  foreach ($next_steps as $next_step => $step_label) {
    if (!empty($submitted[$next_step]['data']['list'])) {
      foreach ($submitted[$next_step]['data']['list'] as $item) {
        if (isset($projects[$item])) {
          $list[] = $projects[$item];
        }
      }
    }
  }

  // It is not possible to make an HTTP request to this same webserver
  // if the host server is PHP itself, because it is single-threaded.
  // See https://www.php.net/manual/en/features.commandline.webserver.php
  $use_http = php_sapi_name() != 'cli-server';
  $php_server = !$use_http;
  if ($php_server) {

    // Log the selected processing method for project support purposes.
    $this->logger
      ->notice('Starting Upgrade Status on @count projects without HTTP sandboxing because the built-in PHP webserver does not allow for that.', [
      '@count' => count($list),
    ]);
  }
  else {

    // Attempt to do an HTTP request to the frontpage of this Drupal instance.
    // If that does not work then we'll not be able to process projects over
    // HTTP. Processing projects directly is less safe (in case of PHP fatal
    // errors the batch process may halt), but we have no other choice here
    // but to take a chance.
    list($error, $message, $data) = static::doHttpRequest('upgrade_status_request_test', 'upgrade_status_request_test');
    if (empty($data) || !is_array($data) || $data['message'] != 'Request test success') {
      $use_http = FALSE;
      $this->logger
        ->notice('Starting Upgrade Status on @count projects without HTTP sandboxing. @error', [
        '@error' => $message,
        '@count' => count($list),
      ]);
    }
  }
  if ($use_http) {

    // Log the selected processing method for project support purposes.
    $this->logger
      ->notice('Starting Upgrade Status on @count projects with HTTP sandboxing.', [
      '@count' => count($list),
    ]);
  }
  foreach ($list as $item) {
    $operations[] = [
      static::class . '::parseProject',
      [
        $item,
        $use_http,
      ],
    ];
  }
  if (!empty($operations)) {

    // Allow other modules to alter the operations to be run.
    $this->moduleHandler
      ->alter('upgrade_status_operations', $operations, $form_state);
  }
  if (!empty($operations)) {
    $batch = [
      'title' => $this
        ->t('Scanning projects'),
      'operations' => $operations,
      'finished' => static::class . '::finishedParsing',
    ];
    batch_set($batch);
  }
  else {
    $this
      ->messenger()
      ->addError('No projects selected to scan.');
  }
}