You are here

twitter_bootstrap_modal.module in Twitter Bootstrap Modal 7.3

Same filename and directory in other branches
  1. 7 twitter_bootstrap_modal.module

Generates a Twitter Bootstrap Modal.

Depends on the jQuery Ajax Load Modules

File

twitter_bootstrap_modal.module
View source
<?php

/**
* @file
* Generates a Twitter Bootstrap Modal.
*
* Depends on the jQuery Ajax Load Modules
*/

/**
 * Implements hook_init().
 */
function twitter_bootstrap_modal_init() {
  $module_path = drupal_get_path('module', 'twitter_bootstrap_modal');
  $tb_trigger = jquery_ajax_load_get_triggers('jquery_ajax_load_trigger_TB', '.twitter_bootstrap_modal');
  $tb_modal_trigger = jquery_ajax_load_get_triggers('jquery_ajax_load_trigger_modal_TB', '.bs_modal');
  $site_name = variable_get('site_name', "Default site name");
  drupal_add_js(array(
    'jquery_ajax_load' => array(
      'TBtrigger' => $tb_trigger,
      'TBmodaltrigger' => $tb_modal_trigger,
      'TBmodule' => $module_path,
      'TBpath' => base_path(),
      'TBname' => $site_name,
    ),
  ), 'setting');
  drupal_add_js($module_path . '/twitter_bootstrap_modal.js');
}

/**
* Implementation of hook_menu().
*/
function twitter_bootstrap_modal_menu() {

  // Admin settings.
  $items['admin/config/development/twitter_bootstrap_modal'] = array(
    'title' => 'Twitter Bootstrap Modal',
    'description' => 'Shows any page on a Twitter Boostrap AJAX Modal',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'twitter_bootstrap_modal_admin',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );

  // Only to generate a TAB
  $items['admin/config/development/twitter_bootstrap_modal/config'] = array(
    'title' => 'Twitter Bootstrap Modal',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  // Prefix for AJAX requests, modal markup added
  $items['bs_modal/jquery_ajax_load/get'] = array(
    'page callback' => 'jquery_ajax_load_callback',
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'access content',
    ),
    'delivery callback' => 'twitter_bootstrap_modal_delivery_callback',
  );
  return $items;
}

/**
 * Callback function for admin setting.
 */
function twitter_bootstrap_modal_admin() {

  // This module will not work if used in overlay paths such as
  // admin/* , node/add etc if user has overlay access.
  $form['jquery_ajax_load_trigger_TB'] = array(
    '#type' => 'textarea',
    '#title' => t('Valid jQuery Classes/IDs to trigger TB Modal via Ajax (One per line)'),
    '#default_value' => variable_get('jquery_ajax_load_trigger_TB', '.twitter_bootstrap_modal' . "\n"),
    '#description' => t('Specify the class/ID of links to convert that link TB Modal, one per line. For example by providing ".twitter_bootstrap_modal" will convert any link with class="twitter_bootstrap_modal"'),
  );
  $form['jquery_ajax_load_trigger_modal_TB'] = array(
    '#type' => 'textarea',
    '#title' => t('Valid jQuery Classes/IDs to tell TB Modal to load with full modal code (One per line)'),
    '#default_value' => variable_get('jquery_ajax_load_trigger_modal_TB', '.tb_modal' . "\n"),
    '#description' => t('Specify the class/ID of links to load TB Modal with full modal HTML wrapper, one per line. For example by providing ".tb_modal" will load full modal HTML in any link with class="tb_modal". If not defined remote call must return modal HTML code.'),
  );
  return system_settings_form($form);
}

/**
 * Returns only content part of page for Delibery Callback function.
 */
function twitter_bootstrap_modal_delivery_callback($page_callback_result) {
  if (isset($page_callback_result) && is_null(drupal_get_http_header('Content-Type'))) {
    drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
  }

  // Send appropriate HTTP-Header for browsers and search engines.
  global $language;
  drupal_add_http_header('Content-Language', $language->language);

  // Menu status constants are integers; page content is a string or array.
  if (is_int($page_callback_result)) {

    // @todo: Break these up into separate functions?
    switch ($page_callback_result) {
      case MENU_NOT_FOUND:
        drupal_add_http_header('Status', '404 Not Found');
        print t("Page not found");
        break;
      case MENU_ACCESS_DENIED:
        drupal_add_http_header('Status', '403 Forbidden');
        print t("Access denied");
        break;
      case MENU_SITE_OFFLINE:
        print t('Site under maintenance');
        break;
    }
  }
  elseif (isset($page_callback_result)) {
    $html = is_string($page_callback_result) ? $page_callback_result : drupal_render($page_callback_result);
    $render_string = theme('twitter_bootstrap_modal_modal', array(
      'site_name' => variable_get('site_name', "Default site name"),
      'render_string' => $html,
    ));
    print $render_string;

    // Perform end-of-request tasks.
    drupal_page_footer();
  }
}

/**
 * Implementation of hook_theme().
 *
 * Creates the template for the TB Modal.
 */
function twitter_bootstrap_modal_theme($existing, $type, $theme, $path) {
  $themes = array(
    'twitter_bootstrap_modal_modal' => array(
      'variables' => array(
        'site_name' => NULL,
        'render_string' => '<span>' . t("No content found") . "</span>",
      ),
      'template' => 'twitter_bootstrap_modal_modal',
    ),
  );
  return $themes;
}

Functions

Namesort descending Description
twitter_bootstrap_modal_admin Callback function for admin setting.
twitter_bootstrap_modal_delivery_callback Returns only content part of page for Delibery Callback function.
twitter_bootstrap_modal_init Implements hook_init().
twitter_bootstrap_modal_menu Implementation of hook_menu().
twitter_bootstrap_modal_theme Implementation of hook_theme().