You are here

site_banner.module in Site Banner 8

Same filename and directory in other branches
  1. 7 site_banner.module
  2. 2.0.x site_banner.module
  3. 1.0.x site_banner.module

Site banner module.

File

site_banner.module
View source
<?php

/**
 * @file
 * Site banner module.
 */
use Drupal\Core\Datetime\DrupalDateTime;

/**
 * Implements hook__page_attachments_alter().
 */
function site_banner_page_attachments_alter(array &$page) {
  $config = \Drupal::configFactory()
    ->get('site_banner.settings');
  $status = $config
    ->get('status') ?? FALSE;
  $header = $config
    ->get('show_header') ?? FALSE;
  $footer = $config
    ->get('show_footer') ?? FALSE;
  if ($status && ($header || $footer)) {
    $page['#attached']['library'][] = 'site_banner/site-banner.print-css';
    $page['#attached']['library'][] = 'site_banner/site-banner.screen-css';
  }
}

/**
 * Implements hook_page_top().
 */
function site_banner_page_top(array &$page_top) {
  $config = \Drupal::configFactory()
    ->get('site_banner.settings');
  $banner_text = $config
    ->get('site_banner_text');
  $status = $config
    ->get('status') ?? FALSE;
  $header = $config
    ->get('show_header') ?? FALSE;
  $footer = $config
    ->get('show_footer') ?? FALSE;
  $backgroundColor = $config
    ->get('site_banner_color') ?? '#FFFFFF';
  $startDate = $config
    ->get('site_banner_start_date');
  $endDate = $config
    ->get('site_banner_end_date');
  $isStart = TRUE;
  $isEnd = FALSE;
  if ($startDate) {
    $startDateTime = new DrupalDateTime($startDate);
    $timeDiff = $startDateTime
      ->getTimestamp() - (new DrupalDateTime('now'))
      ->getTimestamp();
    $isStart = $timeDiff < 0;
  }
  if ($endDate) {
    $endDateTime = new DrupalDateTime($endDate);
    $timeDiff = $endDateTime
      ->getTimestamp() - (new DrupalDateTime('now'))
      ->getTimestamp();
    $isEnd = $timeDiff < 0;
  }
  if ($status && !empty($banner_text) && $isStart && !$isEnd && ($header || $footer)) {
    $headerTag = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#attributes' => [
        'id' => 'site-banner-header-banner',
        'class' => [
          'toolbar',
          'clearfix',
          'toolbar-processed',
          'overlay-displace-top',
        ],
        'style' => 'padding-bottom: 5px; background-color:' . $backgroundColor,
        ';',
      ],
      '#value' => \Drupal\Core\Render\Markup::create($banner_text),
    ];
    $footerTag = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#attributes' => [
        'id' => 'site-banner-footer-banner',
        'class' => [
          'toolbar',
          'clearfix',
          'toolbar-processed',
          'overlay-displace-top',
        ],
        'style' => 'padding-bottom: 5px; background-color:' . $backgroundColor,
        ';',
      ],
      '#value' => $banner_text,
      '#weight' => -100,
    ];
    if ($header) {
      $page_top['site_banner'][] = $headerTag;
    }
    if ($footer) {
      $page_top['site_banner'][] = $footerTag;
    }
  }
}

Functions

Namesort descending Description
site_banner_page_attachments_alter Implements hook__page_attachments_alter().
site_banner_page_top Implements hook_page_top().