function chosen_attach_library in Chosen 8.2
Same name and namespace in other branches
- 3.0.x chosen.module \chosen_attach_library()
Helper function to attach the Chosen library and settings to a given element.
Parameters
array &$element: An render array element.
1 call to chosen_attach_library()
- ChosenFormRender::preRenderSelect in src/
ChosenFormRender.php - Render API callback: Apply Chosen to a select element.
File
- ./
chosen.module, line 66 - General functions and hook implementations.
Code
function chosen_attach_library(array &$element) {
$element['#attached']['library'][] = 'chosen/drupal.chosen';
$chosen_conf = \Drupal::config('chosen.settings');
$css_disabled_themes = $chosen_conf
->get('disabled_themes');
if (empty($css_disabled_themes)) {
$css_disabled_themes = [];
}
// Only add the Chosen CSS if it is not disabled for the active theme.
$theme_name = \Drupal::theme()
->getActiveTheme()
->getName();
if (!in_array($theme_name, $css_disabled_themes, TRUE)) {
$element['#attached']['library'][] = 'chosen_lib/chosen.css';
}
$options = [
'disable_search' => (bool) $chosen_conf
->get('disable_search'),
'disable_search_threshold' => (int) $chosen_conf
->get('disable_search_threshold'),
'allow_single_deselect' => (bool) $chosen_conf
->get('allow_single_deselect'),
'search_contains' => (bool) $chosen_conf
->get('search_contains'),
'placeholder_text_multiple' => $chosen_conf
->get('placeholder_text_multiple'),
'placeholder_text_single' => $chosen_conf
->get('placeholder_text_single'),
'no_results_text' => $chosen_conf
->get('no_results_text'),
'max_shown_results' => $chosen_conf
->get('max_shown_results'),
'inherit_select_classes' => TRUE,
];
$element['#attached']['drupalSettings']['chosen'] = [
'selector' => $chosen_conf
->get('jquery_selector'),
'minimum_single' => (int) $chosen_conf
->get('minimum_single'),
'minimum_multiple' => (int) $chosen_conf
->get('minimum_multiple'),
'minimum_width' => (int) $chosen_conf
->get('minimum_width'),
'use_relative_width' => (bool) $chosen_conf
->get('use_relative_width'),
'options' => $options,
];
}