View source
<?php
define('CHOSEN_WEBSITE_URL', 'https://harvesthq.github.io/chosen');
define('CHOSEN_INCLUDE_ADMIN', 0);
define('CHOSEN_INCLUDE_NO_ADMIN', 1);
define('CHOSEN_INCLUDE_EVERYWHERE', 2);
function chosen_help($path, $arg) {
switch ($path) {
case 'admin/help#chosen':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Chosen uses the Chosen jQuery plugin to make your < select > elements more user-friendly.') . '</p>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<p>' . t('Configure at: <a href="@structure_types">admin/config/user-interface/chosen</a>', array(
'@structure_types' => url('admin/config/user-interface/chosen'),
)) . '</p>';
return $output;
}
}
function chosen_menu() {
$items = array();
$items['admin/config/user-interface/chosen'] = array(
'title' => 'Chosen',
'description' => 'Configuration for Chosen plugin',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'chosen_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'chosen.admin.inc',
);
return $items;
}
function chosen_field_widget_info_alter(&$info) {
$widget_defaults = array(
'options_select' => '',
'select_or_other' => '',
'date_combo' => 0,
);
foreach ($widget_defaults as $widget => $default) {
if (isset($info[$widget])) {
$info[$widget]['settings']['apply_chosen'] = $default;
}
}
}
function chosen_form_field_ui_field_edit_form_alter(&$form, $form_state) {
if (empty($form['#field']['locked']) && isset($form['#instance']['widget']['settings']['apply_chosen'])) {
$form['instance']['widget']['settings']['apply_chosen'] = array(
'#type' => 'select',
'#title' => t('Apply Chosen to the select fields in this widget?'),
'#options' => array(
0 => t('Do not apply'),
1 => t('Apply'),
),
'#default_value' => $form['#instance']['widget']['settings']['apply_chosen'],
'#disabled' => !chosen_get_chosen_path(),
'#empty_option' => t('No preference'),
'#empty_value' => '',
'#chosen' => FALSE,
);
}
}
function chosen_field_widget_form_alter(&$element, &$form_state, $context) {
if (isset($context['instance']['widget']['settings']['apply_chosen'])) {
$value = $context['instance']['widget']['settings']['apply_chosen'];
if ($value === '') {
return;
}
else {
$element['#chosen'] = !empty($value);
}
}
}
function chosen_library() {
global $theme;
$library_path = chosen_get_chosen_path();
$info['chosen'] = array(
'title' => 'Chosen',
'website' => CHOSEN_WEBSITE_URL,
'version' => '1.1.0',
'js' => array(
$library_path . '/chosen.jquery.min.js' => array(),
),
);
$css_disabled_themes = variable_get('chosen_disabled_themes', array());
if (!in_array($theme, $css_disabled_themes, TRUE)) {
$css = $library_path . '/chosen.css';
if (!file_exists($css)) {
$css = $library_path . '/chosen.min.css';
}
$info['chosen']['css'] = array(
$css => array(),
);
}
$options = array(
'allow_single_deselect' => (bool) variable_get('chosen_allow_single_deselect', FALSE),
'disable_search' => (bool) variable_get('chosen_disable_search', FALSE),
'disable_search_threshold' => (int) variable_get('chosen_disable_search_threshold', 0),
'search_contains' => (bool) variable_get('chosen_search_contains', FALSE),
'placeholder_text_multiple' => variable_get('chosen_placeholder_text_multiple', t('Choose some options')),
'placeholder_text_single' => variable_get('chosen_placeholder_text_single', t('Choose an option')),
'no_results_text' => variable_get('chosen_no_results_text', t('No results match')),
'inherit_select_classes' => TRUE,
);
$module_path = drupal_get_path('module', 'chosen');
$info['drupal.chosen'] = array(
'title' => 'Drupal Chosen integration',
'website' => 'https://drupal.org/project/chosen',
'version' => '1.1.0',
'js' => array(
$module_path . '/chosen.js' => array(
'group' => JS_DEFAULT,
'weight' => 100,
),
array(
'data' => array(
'chosen' => array(
'selector' => variable_get('chosen_jquery_selector', 'select:visible'),
'minimum_single' => (int) variable_get('chosen_minimum_single', 20),
'minimum_multiple' => (int) variable_get('chosen_minimum_multiple', 20),
'minimum_width' => (int) variable_get('chosen_minimum_width', ''),
'options' => $options,
),
),
'type' => 'setting',
),
),
'css' => array(
$module_path . '/css/chosen-drupal.css' => array(),
),
'dependencies' => array(
array(
'system',
'jquery.once',
),
array(
'chosen',
'chosen',
),
),
);
return $info;
}
function chosen_element_info_alter(&$info) {
$info['select']['#pre_render'][] = 'chosen_pre_render_select';
if (module_exists('date')) {
$info['date_combo']['#pre_render'][] = 'chosen_pre_render_date_combo';
}
if (module_exists('select_or_other')) {
$info['select_or_other']['#pre_render'][] = 'chosen_pre_render_select_or_other';
}
}
function chosen_pre_render_select($element) {
global $theme, $language;
$is_admin = path_is_admin(current_path()) || current_path() == 'system/ajax' || $theme == variable_get('admin_theme');
$chosen_include = variable_get('chosen_include', CHOSEN_INCLUDE_EVERYWHERE);
if ($chosen_include != CHOSEN_INCLUDE_EVERYWHERE && $is_admin == $chosen_include) {
return $element;
}
if (isset($element['#chosen'])) {
if (!empty($element['#chosen'])) {
$element['#attributes']['class'][] = 'chosen-enable';
}
else {
$element['#attributes']['class'][] = 'chosen-disable';
return $element;
}
}
elseif (isset($element['#attributes']['class']) && is_array($element['#attributes']['class'])) {
if (array_intersect($element['#attributes']['class'], array(
'chosen-disable',
))) {
return $element;
}
elseif (array_intersect($element['#attributes']['class'], array(
'chosen-enable',
'chosen-widget',
))) {
}
}
else {
}
if (isset($element['#field_name']) && !empty($element['#multiple'])) {
unset($element['#options']['_none']);
$field = field_info_field($element['#field_name']);
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && $field['cardinality'] > 1) {
$element['#attributes']['data-cardinality'] = $field['cardinality'];
}
}
if ($language->direction == 1) {
$element['#attributes']['class'][] = 'chosen-rtl';
}
$element['#attached']['library'][] = array(
'chosen',
'drupal.chosen',
);
return $element;
}
function chosen_pre_render_date_combo($element) {
if (isset($element['#chosen'])) {
chosen_element_apply_property_recursive($element, $element['#chosen']);
}
return $element;
}
function chosen_pre_render_select_or_other($element) {
if ($element['#select_type'] == 'select' && isset($element['#chosen'])) {
$element['select']['#chosen'] = $element['#chosen'];
}
return $element;
}
function chosen_element_apply_property_recursive(array &$element, $chosen_value = NULL) {
if (!isset($chosen_value)) {
if (isset($element['#chosen'])) {
$chosen_value = $element['#chosen'];
}
else {
return;
}
}
if (isset($element['#type']) && $element['#type'] == 'select') {
$element['#chosen'] = $chosen_value;
}
foreach (element_children($element) as $key) {
chosen_element_apply_property_recursive($element[$key], $chosen_value);
}
}
function chosen_get_chosen_path() {
if (function_exists('libraries_get_path')) {
return libraries_get_path('chosen');
}
$searchdir = array();
$searchdir[] = 'profiles/' . drupal_get_profile() . '/libraries';
$searchdir[] = 'sites/all/libraries';
$searchdir[] = conf_path() . '/libraries';
foreach ($searchdir as $dir) {
if (file_exists($dir . '/chosen/chosen.jquery.min.js')) {
return $dir . '/chosen';
}
}
return FALSE;
}