You are here

bootstrap_modal_messages.module in Bootstrap Modal Messages 7

Same filename and directory in other branches
  1. 8 bootstrap_modal_messages.module

Main functions for bootstrap_modal_messages module.

File

bootstrap_modal_messages.module
View source
<?php

/**
 * @file
 * Main functions for bootstrap_modal_messages module.
 */
define('BOOTSTRAP_MODAL_MESSAGES_HEADER', '<h4 class="modal-title">' . t('Messages') . '</h4>');
define('BOOTSTRAP_MODAL_MESSAGES_FOOTER', '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>');

/**
 * Implements hook_menu().
 */
function bootstrap_modal_messages_menu() {
  $items = array();
  $items['admin/config/user-interface/bootstrap_modal_messages'] = array(
    'title' => 'Bootstrap Modal Messages',
    'description' => 'Settings for Bootstrap Modal Messages module',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'bootstrap_modal_messages_admin_form',
    ),
    'access arguments' => array(
      'administer bootstrap modal messages',
    ),
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function bootstrap_modal_messages_permission() {
  return array(
    'administer bootstrap modal messages' => array(
      'title' => t('Administer Bootstrap Modal Messages'),
      'description' => t('Configure settings for Bootstrap Modal Messages module.'),
    ),
    'view bootstrap modal messages controls' => array(
      'title' => t('View Bootstrap Modal Messages controls'),
      'description' => t('This is non-administrative.  This is the button to re-show modals.'),
    ),
  );
}

/**
 * Implements hook_help().
 */
function bootstrap_modal_messages_help($path, $arg) {
  switch ($path) {

    // On the help overview page.
    case 'admin/help#bootstrap_modal_messages':
      return '<p>' . t('Transform Drupal messages into Bootstrap Modals. The <a href="@admin">administration page</a> provides settings to a few options.', array(
        '@admin' => url('admin/config/user-interface/bootstrap_modal_messages'),
      )) . '</p>';

    // On the admin settings page.
    case 'admin/config/user-interface/bootstrap_modal_messages':
      return '<p>' . t('This page provides configuration options for bootstrap_modal_messages.module. You can configure how multiple messages are grouped.') . '</p>';
  }
}

/**
 * Admin settings form.
 */
function bootstrap_modal_messages_admin_form($form, &$form_state) {

  // @todo Option to use with any sourced BS library.
  $form['bootstrap_modal_messages_selector'] = array(
    '#type' => 'textfield',
    '#title' => t('Messages div selector'),
    '#default_value' => variable_get('bootstrap_modal_messages_selector', '.messages'),
    '#description' => t('The CSS/JS selector to find messages for the modal.'),
  );
  $form['bootstrap_modal_messages_multiple'] = array(
    '#type' => 'select',
    '#title' => t('Multiple messages setting'),
    '#options' => array(
      'single' => t('Single modal'),
      'multiple' => t('Multiple - New modal per message type'),
    ),
    '#default_value' => variable_get('bootstrap_modal_messages_multiple', 'single'),
    '#description' => t('How to handle multiple messages on one page?'),
  );
  $form['bootstrap_modal_messages_ignore_admin'] = array(
    '#type' => 'select',
    '#title' => t('Ignore admin pages'),
    '#options' => array(
      1 => t('Yes'),
      0 => t('No'),
    ),
    '#default_value' => variable_get('bootstrap_modal_messages_ignore_admin', 1),
    '#description' => t('Ignore admin pages so that messages display normally?'),
  );

  // Modal header.
  $form['modal_header'] = array(
    '#type' => 'fieldset',
    '#title' => t('Modal Header'),
    '#collapsible' => TRUE,
  );
  $form['modal_header']['bootstrap_modal_messages_show_header'] = array(
    '#type' => 'select',
    '#title' => t('Show header'),
    '#options' => array(
      1 => t('Yes'),
      0 => t('No'),
    ),
    '#default_value' => variable_get('bootstrap_modal_messages_show_header', 1),
    '#description' => t('Show the modal header.'),
  );
  $form['modal_header']['bootstrap_modal_messages_title'] = array(
    '#type' => 'textarea',
    '#title' => t('Modal Title'),
    '#default_value' => variable_get('bootstrap_modal_messages_title', BOOTSTRAP_MODAL_MESSAGES_HEADER),
    '#description' => t('Enter the HTML to be used in the modal title. Defaults to Bootstrap\'s default h4 title.'),
  );
  $form['modal_header']['bootstrap_modal_messages_header_close'] = array(
    '#type' => 'select',
    '#title' => t('Show close button (X)'),
    '#options' => array(
      1 => t('Yes'),
      0 => t('No'),
    ),
    '#default_value' => variable_get('bootstrap_modal_messages_header_close', 1),
    '#description' => t('Show the close (X) button in modal header.'),
  );

  // Modal footer.
  $form['modal_footer'] = array(
    '#type' => 'fieldset',
    '#title' => t('Modal Footer'),
    '#collapsible' => TRUE,
  );
  $form['modal_footer']['bootstrap_modal_messages_show_footer'] = array(
    '#type' => 'select',
    '#title' => t('Show footer'),
    '#options' => array(
      1 => t('Yes'),
      0 => t('No'),
    ),
    '#default_value' => variable_get('bootstrap_modal_messages_show_footer', 1),
    '#description' => t('Show the modal footer.'),
  );
  $form['modal_footer']['bootstrap_modal_messages_footer_html'] = array(
    '#type' => 'textarea',
    '#title' => t('Footer HTML'),
    '#default_value' => variable_get('bootstrap_modal_messages_footer_html', BOOTSTRAP_MODAL_MESSAGES_FOOTER),
    '#description' => t('Enter the HTML to be used in the modal footer. Defaults to Bootstrap\'s default Close button.'),
  );

  // Modal controls.
  $form['controls'] = array(
    '#type' => 'fieldset',
    '#title' => t('Controls'),
    '#collapsible' => TRUE,
  );
  $form['controls']['bootstrap_modal_messages_show_onload'] = array(
    '#type' => 'select',
    '#title' => t('Open modal on page load?'),
    '#options' => array(
      1 => t('Yes'),
      0 => t('No - Should enable "Show controls" to display modal'),
    ),
    '#default_value' => variable_get('bootstrap_modal_messages_show_onload', 1),
    '#description' => t('Open modal immediately when the page loads.'),
  );
  $form['controls']['bootstrap_modal_messages_show_controls'] = array(
    '#type' => 'select',
    '#title' => t('Show controls?'),
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
    ),
    '#default_value' => variable_get('bootstrap_modal_messages_show_controls', 0),
    '#description' => t('Creates a div that allows you to show messages again. !note', array(
      '!note' => '<strong>This setting can be overridden by the permission "View Bootstrap Modal Messages controls".</strong>',
    )),
  );
  $form['controls']['bootstrap_modal_messages_controls_html'] = array(
    '#type' => 'textarea',
    '#title' => t('Controls HTML'),
    '#default_value' => variable_get('bootstrap_modal_messages_controls_html', t('Messages')),
    '#description' => t('Enter the HTML to be used in the div for controls.'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_page_build().
 */
function bootstrap_modal_messages_page_build(&$page) {
  $ignore_admin = variable_get('bootstrap_modal_messages_ignore_admin', 1);
  if ($ignore_admin && path_is_admin(current_path())) {
    return;
  }
  $path = drupal_get_path('module', 'bootstrap_modal_messages');

  // Array of settings passed to JS.
  $js_settings = array(
    'selector' => variable_get('bootstrap_modal_messages_selector', '.messages'),
    'show_header' => variable_get('bootstrap_modal_messages_show_header', 1),
    'title' => variable_get('bootstrap_modal_messages_title', BOOTSTRAP_MODAL_MESSAGES_HEADER),
    'header_close' => variable_get('bootstrap_modal_messages_header_close', 1),
    'show_footer' => variable_get('bootstrap_modal_messages_show_footer', 1),
    'footer_html' => variable_get('bootstrap_modal_messages_footer_html', BOOTSTRAP_MODAL_MESSAGES_FOOTER),
    'multiple' => variable_get('bootstrap_modal_messages_multiple', 'single'),
    'show_onload' => variable_get('bootstrap_modal_messages_show_onload', 1),
    'show_controls' => variable_get('bootstrap_modal_messages_show_controls', 0),
    'controls_html' => variable_get('bootstrap_modal_messages_controls_html', t('Messages')),
  );

  // Override setting with permission.
  if (!user_access('view bootstrap modal messages controls')) {
    $js_settings['show_controls'] = 0;
  }

  // Add CSS only if the controls are present.
  if ($js_settings['show_controls']) {
    drupal_add_css($path . '/css/bootstrap_modal_messages.css');
  }

  // Add JS file & settings.
  drupal_add_js($path . '/js/bootstrap_modal_messages.js');
  drupal_add_js(array(
    'bootstrap_modal_messages' => $js_settings,
  ), 'setting');
}

Functions

Constants

Namesort descending Description
BOOTSTRAP_MODAL_MESSAGES_FOOTER
BOOTSTRAP_MODAL_MESSAGES_HEADER @file Main functions for bootstrap_modal_messages module.