You are here

public function UpdaterForm::buildForm in Automatic Updates 8.2

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/UpdaterForm.php, line 85

Class

UpdaterForm
Defines a form to update Drupal core.

Namespace

Drupal\automatic_updates\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $this
    ->messenger()
    ->addWarning($this
    ->t('This is an experimental Automatic Updater using Composer. Use at your own risk.'));
  $form['last_check'] = [
    '#theme' => 'update_last_check',
    '#last' => $this->state
      ->get('update.last_check', 0),
  ];
  $recommender = new UpdateRecommender();
  try {
    $recommended_release = $recommender
      ->getRecommendedRelease(TRUE);
  } catch (\RuntimeException $e) {
    $form['message'] = [
      '#markup' => $e
        ->getMessage(),
    ];
    return $form;
  }

  // @todo Should we be using the Update module's library here, or our own?
  $form['#attached']['library'][] = 'update/drupal.update.admin';

  // If we're already up-to-date, there's nothing else we need to do.
  if ($recommended_release === NULL) {
    $this
      ->messenger()
      ->addMessage('No update available');
    return $form;
  }
  $form['update_version'] = [
    '#type' => 'value',
    '#value' => [
      'drupal' => $recommended_release
        ->getVersion(),
    ],
  ];
  $project = $recommender
    ->getProjectInfo();
  if (empty($project['title']) || empty($project['link'])) {
    throw new \UnexpectedValueException('Expected project data to have a title and link.');
  }
  $title = Link::fromTextAndUrl($project['title'], Url::fromUri($project['link']))
    ->toRenderable();
  switch ($project['status']) {
    case UpdateManagerInterface::NOT_SECURE:
    case UpdateManagerInterface::REVOKED:
      $title['#suffix'] = ' ' . $this
        ->t('(Security update)');
      $type = 'update-security';
      break;
    case UpdateManagerInterface::NOT_SUPPORTED:
      $title['#suffix'] = ' ' . $this
        ->t('(Unsupported)');
      $type = 'unsupported';
      break;
    default:
      $type = 'recommended';
      break;
  }

  // Create an entry for this project.
  $entry = [
    'title' => [
      'data' => $title,
    ],
    'installed_version' => $project['existing_version'],
    'recommended_version' => [
      'data' => [
        // @todo Is an inline template the right tool here? Is there an Update
        // module template we should use instead?
        '#type' => 'inline_template',
        '#template' => '{{ release_version }} (<a href="{{ release_link }}" title="{{ project_title }}">{{ release_notes }}</a>)',
        '#context' => [
          'release_version' => $recommended_release
            ->getVersion(),
          'release_link' => $recommended_release
            ->getReleaseUrl(),
          'project_title' => $this
            ->t('Release notes for @project_title', [
            '@project_title' => $project['title'],
          ]),
          'release_notes' => $this
            ->t('Release notes'),
        ],
      ],
    ],
  ];
  $form['projects'] = [
    '#type' => 'table',
    '#header' => [
      'title' => [
        'data' => $this
          ->t('Name'),
        'class' => [
          'update-project-name',
        ],
      ],
      'installed_version' => $this
        ->t('Installed version'),
      'recommended_version' => [
        'data' => $this
          ->t('Recommended version'),
      ],
    ],
    '#rows' => [
      'drupal' => [
        'class' => "update-{$type}",
        'data' => $entry,
      ],
    ],
  ];

  // @todo Add a hasErrors() or getErrors() method to
  // ReadinessValidationManager to make validation more introspectable.
  // Re-running the readiness checks now should mean that when we display
  // cached errors in automatic_updates_page_top(), we'll see errors that
  // were raised during this run, instead of any previously cached results.
  $errors = $this->readinessValidationManager
    ->run()
    ->getResults(SystemManager::REQUIREMENT_ERROR);
  if (empty($errors)) {
    $form['actions'] = $this
      ->actions();
  }
  return $form;
}