You are here

modal_page.module in Modal 8

Main file for the Modal Page.

File

modal_page.module
View source
<?php

/**
 * @file
 * Main file for the Modal Page.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_page_attachments().
 */
function modal_page_page_attachments(array &$attachments) {
  $config = \Drupal::config('modal_page.settings');
  $modals = $config
    ->get('modals');
  $modals_by_parameter = $config
    ->get('modals_by_parameter');
  if (!empty($modals) || !empty($modals_by_parameter)) {
    $attachments['#attached']['library'][] = 'modal_page/modal-page';
  }
}

/**
 * Implements hook_theme().
 */
function modal_page_theme($existing, $type, $theme, $path) {
  return [
    'modal_page_modal' => [
      'variables' => [
        'do_not_show_again' => NULL,
        'title' => NULL,
        'text' => NULL,
        'button' => NULL,
        'id' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_preprocess_html().
 */
function modal_page_preprocess_html(&$variables) {
  $config = \Drupal::config('modal_page.settings');
  $modals = $config
    ->get('modals');
  $modals_by_parameter = $config
    ->get('modals_by_parameter');
  if (empty($modals) && empty($modals_by_parameter)) {
    return FALSE;
  }
  $modal_to_show = \Drupal::service('modal_page.modals')
    ->checkModalToShow();
  if ($modal_to_show) {
    $do_not_show_again = $modal_to_show['do_not_show_again'];
    $title = $modal_to_show['title'];
    $text = $modal_to_show['text'];
    $button = $modal_to_show['button'];
    $id = $modal_to_show['id'];
    $variables['page_top']['slidedown_templates'] = [
      '#theme' => 'modal_page_modal',
      '#do_not_show_again' => $do_not_show_again,
      '#title' => $title,
      '#text' => $text,
      '#button' => $button,
      '#id' => $id,
    ];
    if (isset($modal_to_show['do_not_show_again']) && !empty($modal_to_show['do_not_show_again'])) {
      $variables['page_top']['slidedown_templates']['#do_not_show_again'] = $modal_to_show['do_not_show_again'];
    }
  }
}

/**
 * Implements hook_help().
 */
function modal_page_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.modal_page':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Modal Page is the easiest and fastest way to put a modal on specific pages. For more information, see the online handbook entry for <a target="_blank" href="@modal_page">Modal Page</a>.', [
        '@modal_page' => 'https://www.drupal.org/project/modal_page',
      ]) . '</p>';
      $output .= '<h3>' . t('Usage') . '</h3>';
      $output .= '<p>' . t('You only need to configure the modal text and on which page it should appear. When someone visits this page, this modal will be displayed.') . '</p>';
      $output .= '<dl>';
      $output .= '<h3>' . t('Displaying modals') . '</h3>';
      $output .= '<p>' . t('The configuration page of Modal Page can be accessed in <i>Configuration</i>  » <i>User interface</i>  » <i>Modal Page</i> screen. When followed the correct pattern, the modal can be displayed on screen very easily.') . '</p>';
      $output .= '<p>' . t('<a target="_blank" href="@modal_page_configuration">Click here</a> to redirect to Modal Page configuration.', [
        '@modal_page_configuration' => base_path() . 'admin/config/user-interface/modal-page/settings',
      ]) . '</p>';
      return $output;
  }
}

Functions

Namesort descending Description
modal_page_help Implements hook_help().
modal_page_page_attachments Implements hook_page_attachments().
modal_page_preprocess_html Implements hook_preprocess_html().
modal_page_theme Implements hook_theme().