You are here

function site_status_message_page_top in Site Status Message 8

Implements hook_page_top().

File

./site_status_message.module, line 34
Site Status Message provides a configurable page top message.

Code

function site_status_message_page_top(array &$page) {
  $config = \Drupal::config('site_status_message.settings');
  $site_status_message = Xss::filter(trim($config
    ->get('message')));
  if ($site_status_message) {

    // Check if we need to show status message on admin pages.
    if (!$config
      ->get('admin') && \Drupal::service('router.admin_context')
      ->isAdminRoute()) {
      return;
    }
    $path = $config
      ->get('link');
    $link = $readmore = '';
    if (!empty($path)) {
      $readmore = Xss::filterAdmin($config
        ->get('readmore'));

      // Build link from user entered data.
      if ($path == '<front>') {
        $link = Link::createFromRoute($readmore, $path);
      }
      else {
        $language = \Drupal::languageManager()
          ->getCurrentLanguage()
          ->getId();
        $path = \Drupal::service('path_alias.manager')
          ->getPathByAlias($path, $language);
        if (\Drupal::service('path.validator')
          ->isValid($path)) {
          if (parse_url($path, PHP_URL_SCHEME) !== NULL) {
            $link = Link::fromTextAndUrl($readmore, Url::fromUri($path));
          }
          else {
            $link = Link::fromTextAndUrl($readmore, Url::fromUserInput($path));
          }
        }
      }
    }
    $page['page_top']['site_status_message'] = [
      '#theme' => 'site_status_message',
      '#message' => $site_status_message,
      '#link' => $link,
      '#readmore' => $readmore,
      '#attached' => [
        'library' => 'site_status_message/site_status_message',
      ],
      '#access' => \Drupal::currentUser()
        ->hasPermission('access content'),
    ];
  }
}