function SystemHooks::pageTop

Implements hook_page_top().

Attributes

#[Hook('page_top')]

File

core/modules/system/src/Hook/SystemHooks.php, line 449

Class

SystemHooks
Hook implementations for system.

Namespace

Drupal\system\Hook

Code

public function pageTop() : void {
  /** @var \Drupal\Core\Routing\AdminContext $admin_context */
  $admin_context = \Drupal::service('router.admin_context');
  if ($admin_context->isAdminRoute() && \Drupal::currentUser()->hasPermission('administer site configuration')) {
    $route_match = \Drupal::routeMatch();
    $route_name = $route_match->getRouteName();
    if ($route_name !== 'system.status' && \Drupal::config('system.advisories')->get('enabled')) {
      /** @var \Drupal\system\SecurityAdvisories\SecurityAdvisoriesFetcher $fetcher */
      $fetcher = \Drupal::service('system.sa_fetcher');
      $advisories = $fetcher->getSecurityAdvisories(FALSE);
      if ($advisories) {
        $messenger = \Drupal::messenger();
        $display_as_errors = FALSE;
        $links = [];
        foreach ($advisories as $advisory) {
          // To ensure that all the advisory messages are grouped together on
          // the page, they must all be warnings or all be errors. If any
          // advisories are not public service announcements, then display all
          // the messages as errors because security advisories already tied
          // to a specific release are immediately actionable by upgrading to
          // a secure version of a project.
          $display_as_errors = $display_as_errors ? TRUE : !$advisory->isPsa();
          $links[] = new Link($advisory->getTitle(), Url::fromUri($advisory->getUrl()));
        }
        foreach ($links as $link) {
          $display_as_errors ? $messenger->addError($link) : $messenger->addWarning($link);
        }
        if (\Drupal::moduleHandler()->moduleExists('help')) {
          $help_link = $this->t('(<a href=":system-help">What are critical security announcements?</a>)', [
            ':system-help' => Url::fromRoute('help.page', [
              'name' => 'system',
            ], [
              'fragment' => 'security-advisories',
            ])->toString(),
          ]);
          $display_as_errors ? $messenger->addError($help_link) : $messenger->addWarning($help_link);
        }
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.