You are here

modal_page.module in Modal 5.0.x

Main file for the Modal Page.

File

modal_page.module
View source
<?php

/**
 * @file
 * Main file for the Modal Page.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
include_once __DIR__ . '/includes/modal_page.install.inc';

/**
 * Implements hook_form_alter().
 */
function modal_page_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'modal_add_form' || $form_id == 'modal_edit_form') {
    $form['#attached']['library'][] = 'modal_page/modal-page-admin';
  }
  return $form;
}

/**
 * Implements hook_page_attachments().
 */
function modal_page_page_attachments(array &$attachments) {
  $config = \Drupal::config('modal_page.settings');
  $load_modal_page_bootstrap = TRUE;
  if (!empty($config
    ->get('no_modal_page_external_js'))) {
    $load_modal_page_bootstrap = FALSE;
  }
  $entity_manager = \Drupal::entityTypeManager();
  $entity_storage = $entity_manager
    ->getStorage('modal');
  $modals = $entity_storage
    ->loadMultiple();
  if (!empty($modals)) {

    // Load Modal Page Library.
    $attachments['#attached']['library'][] = 'modal_page/modal-page';

    // Load Bootstrap Library only if necessary.
    if ($load_modal_page_bootstrap) {
      $attachments['#attached']['library'][] = 'modal_page/modal-page-bootstrap';
    }
  }
}

/**
 * Implements hook_theme().
 */
function modal_page_theme() {
  return [
    'modal_page_modal' => [
      'variables' => [
        'do_not_show_again' => NULL,
        'title' => NULL,
        'text' => NULL,
        'delay_display' => NULL,
        'modal_size' => NULL,
        'button' => NULL,
        'id' => NULL,
        'close_modal_esc_key' => NULL,
        'close_modal_clicking_outside' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_preprocess_html().
 */
function modal_page_preprocess_html(&$variables) {
  $modalToShow = \Drupal::service('modal_page.modals')
    ->checkModalToShow();
  if ($modalToShow) {
    $doNotShowAgain = FALSE;
    if (!empty($modalToShow['do_not_show_again'])) {
      $doNotShowAgain = $modalToShow['do_not_show_again'];
    }
    $title = $modalToShow['title'];
    $text = $modalToShow['text'];
    $delayDisplay = $modalToShow['delay_display'];
    $modalSize = $modalToShow['modal_size'];
    $button = $modalToShow['button'];
    $id = $modalToShow['id'];
    $openModalOnElementClick = $modalToShow['open_modal_on_element_click'];
    $autoOpen = $modalToShow['auto_open'];
    $closeModalEscKey = $modalToShow['close_modal_esc_key'];
    $closeModalClickingOutside = $modalToShow['close_modal_clicking_outside'];
    $variables['page_top']['slidedown_templates'] = [
      '#theme' => 'modal_page_modal',
      '#do_not_show_again' => $doNotShowAgain,
      '#title' => $title,
      '#text' => $text,
      '#delay_display' => $delayDisplay,
      '#modal_size' => $modalSize,
      '#button' => $button,
      '#id' => $id,
      '#close_modal_esc_key' => $closeModalEscKey,
      '#close_modal_clicking_outside' => $closeModalClickingOutside,
    ];
    $variables['#attached']['drupalSettings']['modal_page']['open_modal_on_element_click'] = $openModalOnElementClick;
    $variables['#attached']['drupalSettings']['modal_page']['auto_open'] = $autoOpen;
    if (isset($modalToShow['do_not_show_again']) && !empty($modalToShow['do_not_show_again'])) {
      $variables['page_top']['slidedown_templates']['#do_not_show_again'] = $modalToShow['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>Structure</i>  » <i>Modal</i> screen. When followed the correct pattern, the modal can be displayed on screen very easily.') . '</p>';
      $output .= '<p>' . t('<a href="@modal_page_configuration">Click here</a> to redirect to Modal Page configuration.', [
        '@modal_page_configuration' => \Drupal::urlGenerator()
          ->generateFromRoute('modal_page.settings'),
      ]) . '</p>';
      return $output;
  }
}

Functions

Namesort descending Description
modal_page_form_alter Implements hook_form_alter().
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().