View source
<?php
define('CHOSEN_WIDGET', 'options_select');
function chosen_init() {
drupal_add_css(libraries_get_path('chosen') . '/chosen/chosen.css');
drupal_add_js(chosen_js_path());
drupal_add_js(array(
'chosen' => array(
'selector' => variable_get('chosen_jquery_selector', 'select:visible'),
'minimum' => variable_get('chosen_minimum', 20),
'minimum_width' => variable_get('chosen_minimum_width', 200),
),
), 'setting');
drupal_add_js(drupal_get_path('module', 'chosen') . '/chosen.js');
}
function chosen_menu() {
$items = array();
$items['admin/settings/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_form_field_ui_field_edit_form_alter(&$form, $form_state) {
$type = $form['instance']['widget']['type']['#value'];
if ($type == CHOSEN_WIDGET) {
$field = $form['#field'];
$instance = field_info_instance($form['instance']['entity_type']['#value'], $form['instance']['field_name']['#value'], $form['instance']['bundle']['#value']);
$form['instance'] += chosen_field_widget_settings_form($field, $instance);
}
}
function chosen_field_widget_info_alter(&$info) {
$settings = array(
'apply_chosen' => 0,
);
if (isset($info[CHOSEN_WIDGET]['settings'])) {
$info[CHOSEN_WIDGET]['settings'] += $settings;
}
}
function chosen_field_widget_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $widget['settings'];
$form['chosen'] = array(
'#type' => 'fieldset',
'#title' => t('Chosen'),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
'#parents' => array(
'instance',
'widget',
'settings',
),
);
$form['chosen']['apply_chosen'] = array(
'#type' => 'checkbox',
'#title' => t('Apply Chosen on this field'),
'#default_value' => $settings['apply_chosen'],
);
return $form;
}
function chosen_field_widget_form_alter(&$element, &$form_state, $context) {
$type = $context['instance']['widget']['type'];
if ($type == CHOSEN_WIDGET) {
$settings = $context['instance']['widget']['settings'];
if ($settings['apply_chosen']) {
$element['#attributes']['class'][] = 'chosen-widget';
}
}
}
function chosen_js_path() {
$path = libraries_get_path('chosen');
if (file_exists($path . '/chosen/chosen.jquery.min.js')) {
return $path . '/chosen/chosen.jquery.min.js';
}
elseif (file_exists($path . '/chosen/chosen.jquery.js')) {
return $path . '/chosen/chosen.jquery.js';
}
return FALSE;
}