You are here

jquery_ajax_load.module in jQuery AJAX Load 7

Basic Module file.

File

jquery_ajax_load.module
View source
<?php

/**
* @file
* Basic Module file.
*
*/

/**
 * Implements hook_init().
 */
function jquery_ajax_load_init() {
  $module_path = drupal_get_path('module', 'jquery_ajax_load');
  $trigger = jquery_ajax_load_get_triggers('jquery_ajax_load_trigger', '.jquery_ajax_load');
  $target = variable_get('jquery_ajax_load_target', '#jquery_ajax_load_target');
  $toggle = variable_get('jquery_ajax_load_toggle', TRUE);
  $animation = variable_get('jquery_ajax_load_animation', TRUE);
  $load_css = variable_get('jquery_ajax_load_css', TRUE);
  $load_js = variable_get('jquery_ajax_load_js', TRUE);
  $site_name = variable_get('site_name', "Default site name");
  drupal_add_js(array(
    'jquery_ajax_load' => array(
      'trigger' => $trigger,
      'target' => $target,
      'toggle' => $toggle,
      'animation' => $animation,
      'base_path' => base_path(),
      'module_path' => $module_path,
      'site_name' => $site_name,
    ),
  ), 'setting');
  drupal_add_js($module_path . '/jquery_ajax_load.js');
}

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

  // Admin settings.
  $items['admin/config/development/jquery_ajax_load'] = array(
    'title' => 'JQuery AJAX Load',
    'description' => 'Shows any page on a AJAX link',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'jquery_ajax_load_admin',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );

  // Only to generate a TAB
  $items['admin/config/development/jquery_ajax_load/link'] = array(
    'title' => 'JQuery AJAX Load',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

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

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

  // This module will not work if used in overlay paths such as
  // admin/* , node/add etc if user has overlay access.
  if (user_access('access overlay')) {
    $message = t("Overlay module is not compatible with jQuery Ajax Load links API module.");
    drupal_set_message($message, 'warning', FALSE);
  }
  $form['jquery_ajax_load_trigger'] = 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', '.jquery_ajax_load' . "\n"),
    '#description' => t('Specify the class/ID of links to convert that link to AJAX, one per line. For example by providing ".jquery_ajax_load" will convert any link with class="jquery_ajax_load"'),
  );
  $form['jquery_ajax_load_target'] = array(
    '#type' => 'textfield',
    '#title' => t('A valid jQuery ID where AJAX callback will be rendered'),
    '#default_value' => variable_get('jquery_ajax_load_target', '#jquery_ajax_load_target'),
    '#size' => 60,
    '#maxlength' => 128,
  );
  $form['jquery_ajax_load_toggle'] = array(
    '#type' => 'checkbox',
    '#title' => t('Check if you want link to act as toggle buttom'),
    '#default_value' => variable_get('jquery_ajax_load_toggle', TRUE),
    '#description' => t('If toggle is activated, content on target will desappear when link is clicked twice.'),
  );
  $form['jquery_ajax_load_animation'] = array(
    '#type' => 'checkbox',
    '#title' => t('Check if you want link to use jQuery show and hide effects'),
    '#default_value' => variable_get('jquery_ajax_load_animation', TRUE),
    '#description' => t('If animation is activated, content on target will show and desappear using jQuery show and hide standard effects.'),
  );
  $form['jquery_ajax_load_css'] = array(
    '#type' => 'checkbox',
    '#title' => t('Check if you want to load css files'),
    '#default_value' => variable_get('jquery_ajax_load_css', FALSE),
    '#description' => t('If CSS loading is enabled AJAX will call CSS files and loaded with content. This affects performance.'),
  );
  $form['jquery_ajax_load_js'] = array(
    '#type' => 'checkbox',
    '#title' => t('Check if you want to load js files'),
    '#default_value' => variable_get('jquery_ajax_load_js', FALSE),
    '#description' => t('If JS loading is enabled AJAX will call JS files and loaded with content. This affects performance.'),
  );
  return system_settings_form($form);
}

/**
 * Returns only content part of page for Delivery Callback function.
 */
function jquery_ajax_load_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;
      default:
        print t('Unknown error');
        break;
    }
  }
  elseif (isset($page_callback_result)) {
    $content = is_string($page_callback_result) ? $page_callback_result : drupal_render($page_callback_result);
    $html = '<html><head><title></title>';
    if (variable_get('jquery_ajax_load_css', TRUE)) {
      $html .= drupal_get_css();
    }
    if (variable_get('jquery_ajax_load_js', TRUE)) {
      $html .= drupal_get_js();
    }
    $html .= '</head><body class="jquery-ajax-load">' . $content . '</body></html>';
    print $html;
    drupal_page_footer();
  }
}

/**
 * Callback function for AJAX request.
 *
 * Rebuilds URL for page callback
 * Uses menu_execute_active_handler to build page
 */
function jquery_ajax_load_callback() {

  // Get the arguments from this fuction
  $args = func_get_args();

  // In case the installation is on subdirectory
  $base_path = base_path();
  if (strlen($base_path) > 1) {
    $sub_paths = explode('/', $base_path);

    // Takes out blank items generated by initial slash
    array_shift($sub_paths[0]);
    foreach ($sub_paths as $sub_path) {
      array_shift($args[0]);
    }
  }

  // Verify is language prefix present and take out of URL
  if (module_exists('i18n')) {
    $languages = language_list('enabled');

    // Take only enabled
    $languages = $languages[1];
    if (array_key_exists($args[0], $languages)) {
      $language = $args[0];
      array_shift($args[0]);
    }
    else {
      $language = NULL;
    }
  }
  else {
    $language = NULL;
  }
  $path = implode('/', $args);
  $normal_path = drupal_get_normal_path($path, $language);
  $args = explode('/', $normal_path);

  // Evaluates if entity type have a special view mode
  // If function does not exists returns default view mode or rendered node
  $function = 'jquery_ajax_load_view_' . $args[0];
  if (function_exists($function)) {
    $render_string = $function($args[1]);
  }
  else {
    $render_string = menu_execute_active_handler($normal_path, FALSE);
  }
  return $render_string;
}

/**
 * Get trigger classes/ids.
 */
function jquery_ajax_load_get_triggers($variable, $default_trigger) {
  $trigger = variable_get($variable, $default_trigger . "\n");
  $trigger = explode("\n", $trigger);
  $trigger = array_filter($trigger);
  $trigger = implode(',', $trigger);
  return $trigger;
}

Functions

Namesort descending Description
jquery_ajax_load_admin Callback function for admin setting.
jquery_ajax_load_callback Callback function for AJAX request.
jquery_ajax_load_delivery_callback Returns only content part of page for Delivery Callback function.
jquery_ajax_load_get_triggers Get trigger classes/ids.
jquery_ajax_load_init Implements hook_init().
jquery_ajax_load_menu Implementation of hook_menu().