You are here

chosen_lib.module in Chosen 3.0.x

Same filename and directory in other branches
  1. 8.2 modules/chosen_lib/chosen_lib.module

General functions and hook implementations.

File

modules/chosen_lib/chosen_lib.module
View source
<?php

/**
 * @file
 * General functions and hook implementations.
 *
 * @see https://harvesthq.github.com/chosen/
 */

/**
 * Implements hook_library_info_alter().
 */
function chosen_lib_library_info_alter(array &$libraries, $module) {
  if ('chosen_lib' == $module) {
    if (isset($libraries['chosen'])) {

      // Set chosen library path.
      $chosen_js_path = _chosen_lib_get_chosen_path();
      $minified = file_exists($chosen_js_path . '/chosen.jquery.min.js');
      $chosen_js_path = '/' . $chosen_js_path;
      $chosen_js = $minified ? $chosen_js_path . '/chosen.jquery.min.js' : $chosen_js_path . '/chosen.jquery.js';
      $libraries['chosen']['js'][$chosen_js] = [
        'minified' => $minified,
      ];
      $chosen_css = $chosen_js_path . '/chosen.css';
      $libraries['chosen.css']['css']['component'][$chosen_css] = [];
    }
  }
}

/**
 * Get the location of the chosen library.
 *
 * @return string
 *   The location of the library, or FALSE if the library isn't installed.
 */
function _chosen_lib_get_chosen_path() {

  // The following logic is taken from libraries_get_libraries()
  $searchdir = [];

  // Similar to 'modules' and 'themes' directories inside an installation
  // profile, installation profiles may want to place libraries into a
  // 'libraries' directory.
  $searchdir[] = 'profiles/' . \Drupal::installProfile() . '/libraries';

  // Always search libraries.
  $searchdir[] = 'libraries';

  // Also search sites/<domain>/*.
  $searchdir[] = \Drupal::service('site.path') . '/libraries';
  foreach ($searchdir as $dir) {
    if (file_exists($dir . '/chosen/chosen.jquery.min.js') || file_exists($dir . '/chosen/chosen.jquery.js')) {
      return $dir . '/chosen';
    }
    if (file_exists($dir . '/chosen-js/chosen.jquery.min.js') || file_exists($dir . '/chosen-js/chosen.jquery.js')) {
      return $dir . '/chosen-js';
    }
  }
  return FALSE;
}

Functions

Namesort descending Description
chosen_lib_library_info_alter Implements hook_library_info_alter().
_chosen_lib_get_chosen_path Get the location of the chosen library.