From 4594175da1e065ab3feaef56dc8debb9ae159736 Mon Sep 17 00:00:00 2001 From: drishu Date: Fri, 13 Jan 2023 09:43:44 +0200 Subject: [PATCH] OEL-1574: Enable oebt version of slim_select. --- oe_whitelabel.theme | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/oe_whitelabel.theme b/oe_whitelabel.theme index 4643d41de..d79cda911 100644 --- a/oe_whitelabel.theme +++ b/oe_whitelabel.theme @@ -184,3 +184,53 @@ function oe_whitelabel_preprocess_media__document__default(&$variables) { $variables['translations'][] = $wrapper->toFileValueObject(); } } + +/** + * Implements hook_element_info_alter(). + */ +function oe_whitelabel_element_info_alter(array &$info): void { + $module_handler = \Drupal::service('module_handler'); + + if (isset($info['select']) && $module_handler->moduleExists('slim_select')) { + $info['select']['#process'][] = '_oe_whitelabel_process_element_select'; + } +} + +/** + * Process callback for applying slim_select on elements. + * + * @see oe_whitelabel_element_info_alter + */ +function _oe_whitelabel_process_element_select(&$element) { + if (!$element['#multiple']) { + return $element; + } + + $element['#attributes']['class'][] = 'multi-select'; + // Config is empty so that it uses the defaults. + $element['#slim_select'] = []; + + return $element; +} + +/** + * Implements hook_library_info_alter(). + */ +function oe_whitelabel_library_info_alter(array &$libraries, string $extension): void { + if ('slim_select' !== $extension) { + return; + } + + $theme_handler = \Drupal::service('theme_handler'); + $theme_path = $theme_handler->getTheme('oe_bootstrap_theme')->getPath(); + $path = '/' . $theme_path . '/assets/js/slimselect.min.js'; + + $libraries['slim.select']['js'] = [ + $path => [ + 'minified' => TRUE, + 'attributes' => [ + 'defer' => TRUE, + ], + ], + ]; +}