You are here

modal_page.module in Modal 8.3

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;

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

/**
 * Implements hook_page_attachments().
 */
function modal_page_page_attachments(array &$attachments) {
  $correct_version = modal_page_is_entity_version();
  if (empty($correct_version)) {
    $roles = \Drupal::currentUser()
      ->getRoles();
    if (in_array("administrator", $roles)) {
      $messenger = \Drupal::messenger();
      $messenger
        ->addWarning(t('Modal Page use Entity and Exists pending update(s). Please run /update.php'));
    }
    return FALSE;
  }
  $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;
  }
  $modal_exists = \Drupal::entityQuery('modal_page_modal')
    ->range(0, 1)
    ->execute();
  if (!empty($modal_exists)) {

    // 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) {
  $correct_version = modal_page_is_entity_version();
  if (empty($correct_version)) {
    return FALSE;
  }
  $modal_exists = \Drupal::entityQuery('modal_page_modal')
    ->range(0, 1)
    ->execute();
  if (empty($modal_exists)) {
    return FALSE;
  }
  $modal_to_show = \Drupal::service('modal_page.modals')
    ->checkModalToShow();
  if ($modal_to_show) {
    $do_not_show_again = FALSE;
    if (!empty($modal_to_show['do_not_show_again'])) {
      $do_not_show_again = $modal_to_show['do_not_show_again'];
    }
    $title = $modal_to_show['title'];
    $text = $modal_to_show['text'];
    $delay_display = $modal_to_show['delay_display'];
    $modal_size = $modal_to_show['modal_size'];
    $button = $modal_to_show['button'];
    $id = $modal_to_show['id'];
    $open_modal_on_element_click = $modal_to_show['open_modal_on_element_click'];
    $auto_open = $modal_to_show['auto_open'];
    $close_modal_esc_key = $modal_to_show['close_modal_esc_key'];
    $close_modal_clicking_outside = $modal_to_show['close_modal_clicking_outside'];
    $variables['page_top']['slidedown_templates'] = [
      '#theme' => 'modal_page_modal',
      '#do_not_show_again' => $do_not_show_again,
      '#title' => $title,
      '#text' => $text,
      '#delay_display' => $delay_display,
      '#modal_size' => $modal_size,
      '#button' => $button,
      '#id' => $id,
      '#close_modal_esc_key' => $close_modal_esc_key,
      '#close_modal_clicking_outside' => $close_modal_clicking_outside,
    ];
    $variables['#attached']['drupalSettings']['modal_page']['open_modal_on_element_click'] = $open_modal_on_element_click;
    $variables['#attached']['drupalSettings']['modal_page']['auto_open'] = $auto_open;
    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'];
    }
  }
}

/**
 * Check if Modal Page use Entity Version.
 */
function modal_page_is_entity_version() {
  $schema_version = drupal_get_installed_schema_version('modal_page');
  if (!empty($schema_version) && $schema_version <= 8000) {
    return FALSE;
  }
  return TRUE;
}

/**
 * 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_is_entity_version Check if Modal Page use Entity Version.
modal_page_page_attachments Implements hook_page_attachments().
modal_page_preprocess_html Implements hook_preprocess_html().
modal_page_theme Implements hook_theme().