You are here

function _automatic_updates_checker_requirements in Automatic Updates 8

Same name and namespace in other branches
  1. 7 automatic_updates.install \_automatic_updates_checker_requirements()

Display requirements from results of readiness checker.

Parameters

array $requirements: The requirements array.

1 call to _automatic_updates_checker_requirements()
automatic_updates_requirements in ./automatic_updates.install
Implements hook_requirements().

File

./automatic_updates.install, line 49
Automatic updates install file.

Code

function _automatic_updates_checker_requirements(array &$requirements) {

  /** @var \Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManagerInterface $checker */
  $checker = \Drupal::service('automatic_updates.readiness_checker');
  if (!$checker
    ->isEnabled()) {
    return;
  }
  $last_check_timestamp = $checker
    ->timestamp();
  $requirements['automatic_updates_readiness'] = [
    'title' => t('Update readiness checks'),
    'severity' => REQUIREMENT_OK,
    'value' => t('Your site is ready to for <a href="@readiness_checks">automatic updates</a>.', [
      '@readiness_checks' => 'https://www.drupal.org/docs/8/update/automatic-updates#readiness-checks',
    ]),
  ];
  $error_results = $checker
    ->getResults(ReadinessCheckerManagerInterface::ERROR);
  $warning_results = $checker
    ->getResults(ReadinessCheckerManagerInterface::WARNING);
  $checker_results = array_merge($error_results, $warning_results);
  if (!empty($checker_results)) {
    $requirements['automatic_updates_readiness']['severity'] = $error_results ? REQUIREMENT_ERROR : REQUIREMENT_WARNING;
    $requirements['automatic_updates_readiness']['value'] = new PluralTranslatableMarkup(count($checker_results), '@count check failed:', '@count checks failed:');
    $requirements['automatic_updates_readiness']['description'] = [
      '#theme' => 'item_list',
      '#items' => $checker_results,
    ];
  }
  if (\Drupal::time()
    ->getRequestTime() > $last_check_timestamp + ReadinessCheckerManagerInterface::LAST_CHECKED_WARNING) {
    $requirements['automatic_updates_readiness']['severity'] = REQUIREMENT_ERROR;
    $requirements['automatic_updates_readiness']['value'] = t('Your site has not recently checked if it is ready to apply <a href="@readiness_checks">automatic updates</a>.', [
      '@readiness_checks' => 'https://www.drupal.org/docs/8/update/automatic-updates#readiness-checks',
    ]);
    $readiness_check = Url::fromRoute('automatic_updates.update_readiness');
    $time_ago = \Drupal::service('date.formatter')
      ->formatTimeDiffSince($last_check_timestamp);
    if ($last_check_timestamp === 0) {
      $requirements['automatic_updates_readiness']['description'] = t('<a href="@link">Run readiness checks</a> manually.', [
        '@link' => $readiness_check
          ->toString(),
      ]);
    }
    elseif ($readiness_check
      ->access()) {
      $requirements['automatic_updates_readiness']['description'] = t('Last run @time ago. <a href="@link">Run readiness checks</a> manually.', [
        '@time' => $time_ago,
        '@link' => $readiness_check
          ->toString(),
      ]);
    }
    else {
      $requirements['automatic_updates_readiness']['description'] = t('Readiness checks were last run @time ago.', [
        '@time' => $time_ago,
      ]);
    }
  }
}