Skip to content

Commit

Permalink
Issue #2693649 by jhedstrom, kevin.dutra: Chosen library is included …
Browse files Browse the repository at this point in the history
…in every page request
  • Loading branch information
jhedstrom authored and balint-nagy committed Mar 31, 2016
1 parent 380cdef commit 6565968
Showing 1 changed file with 43 additions and 39 deletions.
82 changes: 43 additions & 39 deletions chosen.module
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,6 @@ use Drupal\field\Entity\FieldConfig;
*/
define('CHOSEN_WEBSITE_URL', 'http://harvesthq.github.io/chosen');

/**
* Implements hook_page_attachments().
*/
function chosen_page_attachments(array &$page) {
$page['#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 = array();
}

// 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)) {
$page['#attached']['library'][] = 'chosen_lib/chosen.css';
}

$options = array(
'disable_search' => (bool) $chosen_conf->get('disable_search'),
'disable_search_threshold' => (int) $chosen_conf->get('disable_search_threshold'),
'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'),
'inherit_select_classes' => TRUE,
);

$page['#attached']['drupalSettings']['chosen'] = array(
'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'),
'options' => $options,
);
}

/**
* Implements hook_element_info_alter().
*/
Expand Down Expand Up @@ -117,11 +79,53 @@ function chosen_pre_render_select($element) {
}
}

$element['#attached']['library'][] = 'chosen/drupal.chosen';
// Attach the library.
chosen_attach_library($element);

return $element;
}

/**
* Helper function to attach the Chosen library and settings to a given element.
*
* @param array &$element
* An render array element.
*/
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 = array();
}

// 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 = array(
'disable_search' => (bool) $chosen_conf->get('disable_search'),
'disable_search_threshold' => (int) $chosen_conf->get('disable_search_threshold'),
'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'),
'inherit_select_classes' => TRUE,
);

$element['#attached']['drupalSettings']['chosen'] = array(
'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'),
'options' => $options,
);
}

/**
* Render API callback: Apply Chosen to a date_combo element.
*
Expand Down

0 comments on commit 6565968

Please sign in to comment.