You are here

site_status_message.module in Site Status Message 8

Same filename and directory in other branches
  1. 7 site_status_message.module

Site Status Message provides a configurable page top message.

@author: Gideon Cresswell (DrupalGideon) <https://www.drupal.org/u/drupalgideon>

File

site_status_message.module
View source
<?php

/**
 * @file
 * Site Status Message provides a configurable page top message.
 *
 * @author: Gideon Cresswell (DrupalGideon)
 *          <https://www.drupal.org/u/drupalgideon>
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
 * Implements hook_help().
 */
function site_status_message_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.site_status_message':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Site Status Message is a simple module to display a site wide message to your users at the top of each page. Use cases could be to inform of known downtime in the future, to advertise a special offer on the site or some important news that needs highlighting.') . '</p>';
      $output .= '<p>' . t('An optional link to a page with more information can be displayed after the message.') . '</p>';
      $output .= '<p>' . t('The settings for this module can be configured on the <a href="@url">Site Information</a> page.', [
        '@url' => '/admin/config/system/site-information#edit-site-status',
      ]) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_page_top().
 */
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'),
    ];
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function site_status_message_form_system_site_information_settings_alter(&$form, &$form_state, $form_id) {
  $config = \Drupal::config('site_status_message.settings');
  $form['site_status'] = [
    '#type' => 'fieldset',
    '#title' => 'Site Status',
    'site_status_message_message' => [
      '#type' => 'textfield',
      '#maxlength' => 256,
      '#title' => 'Status message',
      '#default_value' => $config
        ->get('message'),
      '#description' => t('A message to display at the top of each page.'),
    ],
    'site_status_message_link' => [
      '#type' => 'textfield',
      '#size' => 40,
      '#maxlength' => 256,
      '#title' => 'More information page',
      '#default_value' => $config
        ->get('link'),
      '#description' => t('An optional link to show more information about the status message.'),
    ],
    'site_status_message_readmore' => [
      '#type' => 'textfield',
      '#size' => 40,
      '#maxlength' => 128,
      '#title' => 'More information link text',
      '#default_value' => $config
        ->get('readmore'),
      '#description' => t('The text to display on the "More Information" link.'),
    ],
    'site_status_message_admin' => [
      '#type' => 'checkbox',
      '#title' => 'Show on Admin pages',
      '#default_value' => $config
        ->get('admin'),
    ],
  ];
  $form['#validate'][] = 'site_status_message_form_validate';
  $form['#submit'][] = 'site_status_message_form_submit';
}

/**
 * Validates the submitted site-information form.
 */
function site_status_message_form_validate($form, FormStateInterface &$form_state) {

  // Validate that the More Information page exists.
  $raw_link = $link = $form_state
    ->getValue('site_status_message_link');
  if (!empty($link)) {
    if (parse_url($link, PHP_URL_SCHEME) === NULL) {
      if (strpos($link, '<front>') === 0) {
        $link = '/' . substr($link, strlen('<front>'));
      }
      $link = 'internal:' . $link;
    }
    if (parse_url($link, PHP_URL_SCHEME) === 'internal' && !in_array($raw_link[0], [
      '/',
      '?',
      '#',
    ], TRUE) && substr($raw_link, 0, 7) !== '<front>') {
      $form_state
        ->setErrorByName('site_status_message_link', t('Manually entered paths should start with %slash, %question or %hash.', [
        '%slash' => '/',
        '%question' => '?',
        '%hash' => '#',
      ]));
    }
    $language = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
    if (substr($raw_link, 0, 7) !== '<front>' && !\Drupal::service('path.validator')
      ->isValid(\Drupal::service('path_alias.manager')
      ->getPathByAlias($raw_link, $language))) {
      $form_state
        ->setErrorByName('site_status_message_link', t('You must enter a valid path.'));
    }
  }
}

/**
 * Save the Site Status Message settings.
 */
function site_status_message_form_submit($form, FormStateInterface &$form_state) {
  \Drupal::configFactory()
    ->getEditable('site_status_message.settings')
    ->set('message', $form_state
    ->getValue('site_status_message_message'))
    ->set('link', $form_state
    ->getValue('site_status_message_link'))
    ->set('readmore', $form_state
    ->getValue('site_status_message_readmore'))
    ->set('admin', $form_state
    ->getValue('site_status_message_admin'))
    ->save();
}

/**
 * Implements hook_theme().
 */
function site_status_message_theme($existing, $type, $theme, $path) {
  return [
    'site_status_message' => [
      'variables' => [
        'message' => NULL,
        'link' => NULL,
        'readmore' => NULL,
      ],
      'render element' => 'element',
      'template' => 'site-status-message',
    ],
  ];
}

/**
 * Implements hook_config_translation_info_alter().
 */
function site_status_message_config_translation_info_alter(&$info) {
  $info['system.site_information_settings']['names'][] = 'site_status_message.settings';
}