Skip to content

Commit

Permalink
Issue #2410783 by mr.york, nagy.balint: Make the 'Use the default cho…
Browse files Browse the repository at this point in the history
…sen theme' a per theme setting
  • Loading branch information
york authored and balint-nagy committed Mar 3, 2016
1 parent cb0ebf9 commit 61a5a40
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
12 changes: 12 additions & 0 deletions chosen.module
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ function chosen_page_attachments(array &$page) {
$page['#attached']['library'][] = 'chosen/drupal.chosen';

$chosen_conf = \Drupal::config('chosen.settings');

$css_disabled_themes = $chosen_conf->get('chosen_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('chosen_disable_search'),
'disable_search_threshold' => (int) $chosen_conf->get('chosen_disable_search_threshold'),
Expand Down
1 change: 0 additions & 1 deletion config/install/chosen.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ chosen_minimum_width: 200
chosen_jquery_selector: select:visible
chosen_search_contains: FALSE
chosen_disable_search: FALSE
chosen_use_theme: TRUE
chosen_placeholder_text_multiple: Choose some option
chosen_placeholder_text_single: Choose an option
chosen_no_results_text: No results match
8 changes: 5 additions & 3 deletions config/schema/chosen.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ chosen.settings:
chosen_disable_search:
type: boolean
label: 'Disable search box'
chosen_use_theme:
type: boolean
label: 'Use the default chosen theme'
chosen_disabled_themes:
type: sequence
label: 'Disable the default Chosen theme for the following themes'
sequence:
type: string
chosen_placeholder_text_multiple:
type: label
label: 'Placeholder text of multiple selects'
Expand Down
7 changes: 5 additions & 2 deletions modules/chosen_lib/chosen_lib.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ chosen:
gpl-compatible: true
js:
/libraries/chosen/chosen.jquery.min.js: { minified: true }
dependencies:
- core/jquery

chosen.css:
version: 1.5.0
css:
component:
/libraries/chosen/chosen.css: {}
dependencies:
- core/jquery
4 changes: 2 additions & 2 deletions modules/chosen_lib/chosen_lib.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Implements hook_library_info_alter().
*/
function chosen_lib_info_alter(array &$libraries, $module) {
function chosen_lib_library_info_alter(array &$libraries, $module) {
if ('chosen_lib' == $module) {
if (isset($libraries['chosen'])) {
// Set chosen library path.
Expand All @@ -21,7 +21,7 @@ function chosen_lib_info_alter(array &$libraries, $module) {
);

$chosen_css = $chosen_js_path . '/chosen.css';
$libraries['chosen']['css']['component'][$chosen_css] = array();
$libraries['chosen.css']['css']['component'][$chosen_css] = array();
}
}
}
Expand Down
45 changes: 38 additions & 7 deletions src/Form/ChosenConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {

$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Chosen options'),
'#title' => t('Chosen general options'),
);

$form['options']['chosen_search_contains'] = array(
Expand All @@ -112,11 +112,19 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#description' => t('Enable or disable the search box in the results list to filter out possible options.'),
);

$form['options']['chosen_use_theme'] = array(
'#type' => 'checkbox',
'#title' => t('Use the default chosen theme'),
'#default_value' => $chosen_conf->get('chosen_use_theme'),
'#description' => t('Enable or disable the default chosen CSS file. Disable this option if your theme contains custom styles for Chosen replacements.'),
$form['theme_options'] = array(
'#type' => 'fieldset',
'#title' => t('Chosen per theme options'),
);

$default_chosen_disabled_themes = $chosen_conf->get('chosen_disabled_themes');
$default_chosen_disabled_themes = is_array($default_chosen_disabled_themes) ? $default_chosen_disabled_themes : array();
$form['theme_options']['chosen_disabled_themes'] = array(
'#type' => 'checkboxes',
'#title' => t('Disable the default Chosen theme for the following themes'),
'#options' => $this->chosen_enabled_themes_options(),
'#default_value' => $default_chosen_disabled_themes,
'#description' => t('Enable or disable the default Chosen CSS file. Select a theme if it contains custom styles for Chosen replacements.'),
);

$form['strings'] = array(
Expand Down Expand Up @@ -172,7 +180,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('chosen_jquery_selector', $form_state->getValue('chosen_jquery_selector'))
->set('chosen_search_contains', $form_state->getValue('chosen_search_contains'))
->set('chosen_disable_search', $form_state->getValue('chosen_disable_search'))
->set('chosen_use_theme', $form_state->getValue('chosen_use_theme'))
->set('chosen_disabled_themes', $form_state->getValue('chosen_disabled_themes'))
->set('chosen_placeholder_text_multiple', $form_state->getValue('chosen_placeholder_text_multiple'))
->set('chosen_placeholder_text_single', $form_state->getValue('chosen_placeholder_text_single'))
->set('chosen_no_results_text', $form_state->getValue('chosen_no_results_text'));
Expand All @@ -182,4 +190,27 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
}

/**
* Helper function to get options for enabled themes
*/
private function chosen_enabled_themes_options() {
$options = array();

// Get a list of available themes.
$theme_handler = \Drupal::service('theme_handler');

$themes = $theme_handler->listInfo();

foreach ($themes as $theme_name => $theme) {
// Only create options for enabled themes
if ($theme->status) {
if (!(isset($theme->info['hidden']) && $theme->info['hidden'])) {
$options[$theme_name] = $theme->info['name'];
}
}
}

return $options;
}

}

0 comments on commit 61a5a40

Please sign in to comment.