You are here

function chosen_library in Chosen 7.3

Same name and namespace in other branches
  1. 7.2 chosen.module \chosen_library()

Implements hook_library().

Register the chosen libraries, provided by the chosen plugins with all it's custom options. The options need to be extracted from the plugin options formular if no values have yet been saved, otherwise from the chosen_plugin variable.

1 string reference to 'chosen_library'
chosen.inc in plugins/library/chosen.inc

File

./chosen.module, line 255
General functions and hook implementations.

Code

function chosen_library() {
  ctools_include('plugins');
  foreach (ctools_get_plugins('chosen', 'library') as $plugin_name => $plugin_settings) {
    $plugin_class_name = ctools_plugin_load_class('chosen', 'library', $plugin_name, 'class');
    $plugin = new $plugin_class_name();
    $library_path = module_exists('libraries') ? libraries_get_path($plugin->library_name) : 'sites/all/libraries/' . $plugin->directory;

    // Registerl the library.
    $info[$plugin->library_name] = array(
      'title' => $plugin->title,
      'website' => $plugin->website,
      'version' => $plugin->version,
      'js' => array(
        $library_path . '/' . $plugin->file_name => array(
          'group' => 'JS_LIBRARY',
        ),
      ),
    );

    // TODO: Make css optional and make it possible to have multiple css files attached.
    //       Perhaps it would make sense to allow multiple js files too?
    if (variable_get('chosen_use_theme', TRUE)) {
      $info['chosen']['css'] = array(
        $library_path . '/' . $plugin->css => array(),
      );
    }

    // TODO: Refactor this.
    $values = array();
    $value = variable_get('chosen_' . $plugin_name);
    if (!empty($value)) {
      $values = variable_get('chosen_' . $plugin_name);
    }
    else {
      $options = _chosen_flattern_options($plugin
        ->options());
      foreach ($options as $key => $value) {

        // TODO: recursiv.
        $values[$key] = $value['#default_value'];
      }
    }

    // Provide settings for the library.
    // TODO: Get the settings from the plugin.
    $module_path = drupal_get_path('module', 'chosen');
    $info['drupal.' . $plugin_name] = array(
      'title' => 'Drupal Chosen integration',
      'website' => 'http://drupal.org/project/chosen',
      'version' => '1.0',
      'js' => array(
        $module_path . '/chosen.js' => array(),
        array(
          'data' => array(
            'chosen' => array(
              'selector' => variable_get('chosen_jquery_selector', 'select:visible'),
              'minimum_single' => variable_get('chosen_minimum_single', 20),
              'minimum_multiple' => variable_get('chosen_minimum_multiple', 20),
              'minimum_width' => variable_get('chosen_minimum_width', 200),
              'search_contains' => variable_get('chosen_search_contains', FALSE) ? TRUE : FALSE,
              'disable_search' => variable_get('chosen_disable_search', FALSE) ? TRUE : FALSE,
              'disable_search_threshold' => variable_get('chosen_disable_search_threshold', 0),
              'placeholder_text_multiple' => t(variable_get('chosen_placeholder_text_multiple', 'Choose some options')),
              'placeholder_text_single' => t(variable_get('chosen_placeholder_text_single', 'Choose an option')),
              'no_results_text' => t(variable_get('chosen_no_results_text', 'No results match')),
            ),
          ),
          'type' => 'setting',
        ),
      ),
      'css' => array(
        // TODO: Not sure about that.
        $module_path . '/css/chosen-drupal.css' => array(),
      ),
      'dependencies' => array(
        array(
          'chosen',
          $plugin->library_name,
        ),
      ),
    );
  }
  return $info;
}