You are here

function _system_advisories_requirements in Drupal 9

Display requirements from security advisories.

Parameters

array[] $requirements: The requirements array as specified in hook_requirements().

1 call to _system_advisories_requirements()
system_requirements in core/modules/system/system.install
Implements hook_requirements().

File

core/modules/system/system.install, line 1480
Install, update and uninstall functions for the system module.

Code

function _system_advisories_requirements(array &$requirements) : void {
  if (!\Drupal::config('system.advisories')
    ->get('enabled')) {
    return;
  }

  /** @var \Drupal\system\SecurityAdvisories\SecurityAdvisoriesFetcher $fetcher */
  $fetcher = \Drupal::service('system.sa_fetcher');
  try {
    $advisories = $fetcher
      ->getSecurityAdvisories(TRUE, 5);
  } catch (TransferException $exception) {
    $requirements['system_advisories']['title'] = t('Critical security announcements');
    $requirements['system_advisories']['severity'] = REQUIREMENT_WARNING;
    $requirements['system_advisories']['description'] = [
      '#theme' => 'system_security_advisories_fetch_error_message',
    ];
    watchdog_exception('system', $exception, 'Failed to retrieve security advisory data.');
    return;
  }
  if (!empty($advisories)) {
    $advisory_links = [];
    $severity = REQUIREMENT_WARNING;
    foreach ($advisories as $advisory) {
      if (!$advisory
        ->isPsa()) {
        $severity = REQUIREMENT_ERROR;
      }
      $advisory_links[] = new Link($advisory
        ->getTitle(), Url::fromUri($advisory
        ->getUrl()));
    }
    $requirements['system_advisories']['title'] = t('Critical security announcements');
    $requirements['system_advisories']['severity'] = $severity;
    $requirements['system_advisories']['description'] = [
      'list' => [
        '#theme' => 'item_list',
        '#items' => $advisory_links,
      ],
    ];
    if (\Drupal::moduleHandler()
      ->moduleExists('help')) {
      $requirements['system_advisories']['description']['help_link'] = Link::createFromRoute('What are critical security announcements?', 'help.page', [
        'name' => 'system',
      ], [
        'fragment' => 'security-advisories',
      ])
        ->toRenderable();
    }
  }
}