You are here

function automatic_updates_page_top in Automatic Updates 8

Same name and namespace in other branches
  1. 8.2 automatic_updates.module \automatic_updates_page_top()

Implements hook_page_top().

File

./automatic_updates.module, line 18
Contains automatic_updates.module..

Code

function automatic_updates_page_top(array &$page_top) {

  /** @var \Drupal\Core\Routing\AdminContext $admin_context */
  $admin_context = \Drupal::service('router.admin_context');
  $route_match = \Drupal::routeMatch();
  if ($admin_context
    ->isAdminRoute($route_match
    ->getRouteObject()) && \Drupal::currentUser()
    ->hasPermission('administer site configuration')) {
    $disabled_routes = [
      'update.theme_update',
      'system.theme_install',
      'update.module_update',
      'update.module_install',
      'update.status',
      'update.report_update',
      'update.report_install',
      'update.settings',
      'system.status',
      'update.confirmation_page',
    ];

    // These routes don't need additional nagging.
    if (in_array(\Drupal::routeMatch()
      ->getRouteName(), $disabled_routes, TRUE)) {
      return;
    }

    /** @var \Drupal\automatic_updates\Services\AutomaticUpdatesPsaInterface $psa */
    $psa = \Drupal::service('automatic_updates.psa');
    $messages = $psa
      ->getPublicServiceMessages();
    if ($messages) {
      \Drupal::messenger()
        ->addError(t('Public service announcements:'));
      foreach ($messages as $message) {
        \Drupal::messenger()
          ->addError($message);
      }
    }
    $last_check_timestamp = \Drupal::service('automatic_updates.readiness_checker')
      ->timestamp();
    if (\Drupal::time()
      ->getRequestTime() > $last_check_timestamp + ReadinessCheckerManagerInterface::LAST_CHECKED_WARNING) {
      $readiness_settings = Url::fromRoute('automatic_updates.settings');
      \Drupal::messenger()
        ->addError(t('Your site has not recently run an update readiness check. <a href="@link">Administer automatic updates</a> and run readiness checks manually.', [
        '@link' => $readiness_settings
          ->toString(),
      ]));
    }

    /** @var \Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManagerInterface $checker */
    $checker = \Drupal::service('automatic_updates.readiness_checker');
    $results = $checker
      ->getResults(ReadinessCheckerManagerInterface::ERROR);
    if ($results) {
      \Drupal::messenger()
        ->addError(t('Your site is currently failing readiness checks for automatic updates. It cannot be <a href="@readiness_checks">automatically updated</a> until further action is performed:', [
        '@readiness_checks' => 'https://www.drupal.org/docs/8/update/automatic-updates#readiness-checks',
      ]));
      foreach ($results as $message) {
        \Drupal::messenger()
          ->addError($message);
      }
    }
    $results = $checker
      ->getResults('warning');
    if ($results) {
      \Drupal::messenger()
        ->addWarning(t('Your site does not pass some readiness checks for automatic updates. Depending on the nature of the failures, it might effect the eligibility for <a href="@readiness_checks">automatic updates</a>.', [
        '@readiness_checks' => 'https://www.drupal.org/docs/8/update/automatic-updates#readiness-checks',
      ]));
      foreach ($results as $message) {
        \Drupal::messenger()
          ->addWarning($message);
      }
    }
  }
}