You are here

jquery_ui_multiselect_widget.module in jQuery UI MultiSelect Widget 7

jquery_ui_multiselect_widget module.

File

jquery_ui_multiselect_widget.module
View source
<?php

/**
 * @file
 * jquery_ui_multiselect_widget module.
 */

/**
 * Implements HOOK_init().
 *
 * Initialization of jquery_ui_multiselect_widget (on each page).
 * We can not use hook_page_built here, because it doesn't work on AJAX content then.
 */
function jquery_ui_multiselect_widget_init() {
  $module_path = drupal_get_path('module', 'jquery_ui_multiselect_widget');
  $js_settings = _jquery_ui_multiselect_widget_get_settings();
  $path = drupal_strtolower(drupal_get_path_alias(check_plain($_GET['q'])));
  if (!drupal_match_path($path, $js_settings['jquery_ui_multiselect_widget_path_match_exclude'])) {
    if (($library = libraries_load('jquery-ui-multiselect-widget')) && !empty($library['loaded'])) {
      drupal_add_library('system', 'ui.core');
      drupal_add_library('system', 'ui.widget');
      drupal_add_library('system', 'ui.effects.core');

      // Add Settings
      drupal_add_js(array(
        'jquery_ui_multiselect_widget' => $js_settings,
      ), 'setting');

      // Module JS for init as set in config.
      drupal_add_js($module_path . '/jquery_ui_multiselect_widget.js', array(
        'scope' => 'footer',
      ));

      // Module CSS as default
      drupal_add_css($module_path . '/jquery_ui_multiselect_widget.css');
    }
  }
}

/**
 * Implements hook_requirements().
 */
function jquery_ui_multiselect_widget_requirements($phase) {

  // Create an array to hold jQuery webks: Responsive Tables requirements
  $requirements = array();

  // Check requirements during the runtime phase
  if ($phase == 'runtime') {

    // Check if the jQuery webks: Responsive Tables jQuery plugin library is installed
    if (($library = libraries_detect('jquery-ui-multiselect-widget')) && !empty($library['installed'])) {
      $requirements['jquery-ui-multiselect-widget'] = array(
        'title' => t('jQuery UI MultiSelect Widget'),
        'value' => t('Installed'),
        'severity' => REQUIREMENT_OK,
      );
    }
    else {
      $requirements['jquery-ui-multiselect-widget'] = array(
        'title' => t('jQuery UI MultiSelect Widget'),
        'value' => t('Not installed'),
        'description' => $library['error message'],
        'severity' => REQUIREMENT_ERROR,
      );
    }

    // Check if the site is running >= jQuery 1.4
    if (($library = drupal_get_library('system', 'jquery')) && version_compare($library['version'], '1.4', '>=')) {
      $requirements['jquery-ui-multiselect-widget-jquery'] = array(
        'title' => t('jQuery UI MultiSelect Widget'),
        'value' => t('jQuery @version', array(
          '@version' => $library['version'],
        )),
        'severity' => REQUIREMENT_OK,
      );
    }
    else {
      $destination = drupal_get_destination();
      $requirements['jquery-ui-multiselect-widget-jquery'] = array(
        'title' => t('jQuery UI MultiSelect Widget'),
        'value' => t('jQuery @version', array(
          '@version' => $library['version'],
        )),
        'description' => t('jQuery UI MultiSelect Widget requires jQuery 1.4.2 or greater. Configure <a href="@jquery_update">jQuery Update</a>.', array(
          '@jquery_update' => url('admin/config/development/jquery_update', array(
            'query' => $destination,
          )),
        )),
        'severity' => REQUIREMENT_ERROR,
      );
    }
    if (($library = drupal_get_library('system', 'ui')) && version_compare($library['version'], '1.8', '>=')) {
      $requirements['jquery-ui-multiselect-widget-jqueryui'] = array(
        'title' => t('jQuery UI MultiSelect Widget'),
        'value' => t('jQuery UI @version', array(
          '@version' => $library['version'],
        )),
        'severity' => REQUIREMENT_OK,
      );
    }
    else {
      $destination = drupal_get_destination();
      $requirements['jquery-ui-multiselect-widget-jqueryui'] = array(
        'title' => t('jQuery UI MultiSelect Widget'),
        'value' => t('jQuery UI @version', array(
          '@version' => $library['version'],
        )),
        'description' => t('jQuery UI MultiSelect Widget requires jQuery UI 1.8 or greater.'),
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $requirements;
}

/**
 * Implements hook_library().
 */
function jquery_ui_multiselect_widget_libraries_info() {
  $libraries = array();
  $libraries['jquery-ui-multiselect-widget'] = array(
    'name' => 'jQuery UI MultiSelect Widget',
    'vendor url' => 'http://www.erichynds.com/blog/jquery-ui-multiselect-widget',
    'download url' => 'https://github.com/ehynds/jquery-ui-multiselect-widget/archive/master.zip',
    'version' => '1.0',
    'files' => array(
      'js' => array(
        'src/jquery.multiselect.js' => array(
          'scope' => 'footer',
        ),
        'src/jquery.multiselect.filter.js' => array(
          'scope' => 'footer',
        ),
      ),
      'css' => array(
        'jquery.multiselect.css',
        'jquery.multiselect.filter.css',
      ),
    ),
  );
  return $libraries;
}

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

    // Main module help for the block module
    case 'admin/help#jquery_ui_multiselect_widget':
    case 'admin/config/user-interface/jquery_ui_multiselect_widget':
      return '<p>' . t('Adds Eric Hynds jQuery UI MultiSelect Widget (!website) to select fields (optional multiselect only) in Drupal.
        Replaces all selects with attribute "multiselect" by this better behaviour.', array(
        '!website' => l(t('http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/'), 'http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/'),
      ));
  }
}

/**
 * Implements hook_load_menu().
 */
function jquery_ui_multiselect_widget_menu() {
  $items['admin/config/user-interface/jquery_ui_multiselect_widget'] = array(
    'title' => 'jQuery UI MultiSelect Widget',
    'description' => 'Configure jQuery UI MultiSelect Widget settings.',
    'access arguments' => array(
      'administer site configuration',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'jquery_ui_multiselect_widget_settings_form',
    ),
    'file' => 'jquery_ui_multiselect_widget.admin.inc',
  );
  return $items;
}

/**
 * Specifies the commerce_powl AJAX Settings.
 *
 * @return array Array of configuration variables to be made available in
 *         JavaScript.
 */
function _jquery_ui_multiselect_widget_get_settings() {
  $module_path = drupal_get_path('module', 'jquery_ui_multiselect_widget');
  return array(
    'module_path' => $module_path,
    'multiple' => variable_get('jquery_ui_multiselect_widget_multiple', FALSE),
    'filter' => variable_get('jquery_ui_multiselect_widget_filter', FALSE),
    'subselector' => check_plain(variable_get('jquery_ui_multiselect_widget_subselector', '')),
    // Represents the default settings of the jquery module. TODO: Add them to
    // admin configuration.
    'selectedlist' => variable_get('jquery_ui_multiselect_widget_selectedlist', 4),
    'multiple' => variable_get('jquery_ui_multiselect_widget_multiple', TRUE),
    'autoOpen' => variable_get('jquery_ui_multiselect_widget_autoOpen', FALSE),
    'header' => variable_get('jquery_ui_multiselect_widget_header', TRUE),
    'height' => variable_get('jquery_ui_multiselect_widget_height', 175),
    'classes' => variable_get('jquery_ui_multiselect_widget_classes', ''),
    'filter_auto_reset' => variable_get('jquery_ui_multiselect_widget_filter_auto_reset', FALSE),
    'filter_width' => variable_get('jquery_ui_multiselect_widget_filter_width', 100),
    'jquery_ui_multiselect_widget_path_match_exclude' => variable_get('jquery_ui_multiselect_widget_path_match_exclude', ''),
  );
}