You are here

footer_message.module in Footer Message 8

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

This module provides a configurable footer message as a block.

File

footer_message.module
View source
<?php

/**
 * @file
 * This module provides a configurable footer message as a block.
 */
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function footer_message_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.footer_message':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module provides a site footer message, editable on the site-information form and displayed as a block, as well as presented as a theme variable.') . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('The footer message block can be placed on your site via the block administration screen, or with Context module.') . '</dt>';
      $output .= '<dt>' . t('Or, footer_message variable can be added to your page.html.twig.') . '</dt>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function footer_message_form_system_site_information_settings_alter(&$form, FormStateInterface &$form_state) {

  // Add a footer textarea to the "Site Information" admin page.
  $config = \Drupal::config('footer_message.settings');
  $message = $config
    ->get('footer_message_msg');
  $format = $config
    ->get('footer_message_format');
  $form['footer_message_msg'] = [
    '#type' => 'text_format',
    '#title' => t('Site Footer message'),
    '#default_value' => $message ? $message : '',
    '#format' => $format,
  ];
  $form['#submit'][] = 'footer_message_form_system_site_information_settings_submit';
}

/**
 * Custom handler submit for system site information settings form.
 */
function footer_message_form_system_site_information_settings_submit(&$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValue('footer_message_msg');
  $element = $form['footer_message_msg'];
  $config = \Drupal::service('config.factory')
    ->getEditable('footer_message.settings');
  $config
    ->set('footer_message_msg', $values['value'])
    ->set('footer_message_format', $values['format'])
    ->save();

  // Invalidate footer_message block cache tag.
  if ($values['value'] != $element['#default_value'] || $values['format'] != $element['#format']) {
    Cache::invalidateTags([
      'block:footer_message',
    ]);
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function footer_message_preprocess_page(&$variables) {

  // Provide footer_message as a theme variable to hook_preprocess_page()
  // and page.html.twig.
  // Note that we apply filter format before outputting HTML.
  $config = \Drupal::config('footer_message.settings');
  $message = $config
    ->get('footer_message_msg');
  $format = $config
    ->get('footer_message_format');
  $variables['footer_message'] = check_markup($message ? $message : '', $format);
}

/**
 * Implements hook_block_view_BASE_BLOCK_ID_alter().
 */
function footer_message_block_view_footer_message_block_alter(array &$build, BlockPluginInterface $block) {

  // Add contextual link.
  $build['#contextual_links']['footer_message'] = [
    'route_parameters' => [],
  ];
}