diff --git a/conditional_fields.libraries.yml b/conditional_fields.libraries.yml index 4b83c2c42303aadd90d7c9bfc8858768e351da6e..00134124272ad824453d9cca310b87225f239f78 100644 --- a/conditional_fields.libraries.yml +++ b/conditional_fields.libraries.yml @@ -5,6 +5,16 @@ conditional_fields: - core/jquery - core/drupal.states +conditional_fields.select2: + js: + js/conditional_fields.select2.js: {} + dependencies: + - conditional_fields/conditional_fields + - core/drupal + - core/once + - core/jquery + - select2/select2 + admin: css: component: diff --git a/conditional_fields.module b/conditional_fields.module index 8513aa50faf50a5d6ebbede6d491a3e877da27cb..6ef74438817bbf5cc687f9f3b41feb403bee23ce 100644 --- a/conditional_fields.module +++ b/conditional_fields.module @@ -208,3 +208,18 @@ function conditional_fields_get_simpler_id($id) { return $id; } + +/** + * Attaches conditional fields libraries and Select2 integration when the Select2 module is present. + */ +function conditional_fields_page_attachments(array &$attachments) { + $module_handler = \Drupal::service('module_handler'); + + // Attach base library + $attachments['#attached']['library'][] = 'conditional_fields/conditional_fields'; + + // Attach Select2 integration if module is present + if ($module_handler->moduleExists('select2')) { + $attachments['#attached']['library'][] = 'conditional_fields/conditional_fields.select2'; + } +} diff --git a/js/conditional_fields.select2.js b/js/conditional_fields.select2.js new file mode 100644 index 0000000000000000000000000000000000000000..ea2b3ab29a3682e2f880427267031839a563b6a6 --- /dev/null +++ b/js/conditional_fields.select2.js @@ -0,0 +1,76 @@ +(function ($, Drupal) { + 'use strict'; + + Drupal.behaviors.conditionalFieldsSelect2 = { + attach: function (context) { + $(once('conditional-fields-select2', '.select2-widget', context)).each( + function () { + var $select = $(this); + + // Hide all dependent fields initially + $('[data-drupal-states]').each(function () { + var $dependent = $(this); + var $wrapper = $dependent.closest('.js-form-wrapper'); + $wrapper.hide(); + }); + + $select.on( + 'select2:select select2:unselect change.select2', + function (e) { + var selectedValues = $select.val() || []; + if (!Array.isArray(selectedValues)) { + selectedValues = [selectedValues]; + } + + // Find dependent fields + $('[data-drupal-states]').each(function () { + var $dependent = $(this); + var states = $dependent.data('drupal-states'); + + if (!states || !states.visible) { + return; + } + + var visibleStates = $.isArray(states.visible) + ? states.visible + : [states.visible]; + var $wrapper = $dependent.closest('.js-form-wrapper'); + + visibleStates.forEach(function (state) { + if (state.selector === '#' + $select.attr('id')) { + var stateValue = state.value; + if (!Array.isArray(stateValue)) { + stateValue = [stateValue]; + } + + // Check if any of the selected values match the state values + var shouldShow = selectedValues.some(function ( + selectedValue + ) { + return stateValue.includes(selectedValue); + }); + + if (shouldShow) { + $wrapper.show(); + } else { + $wrapper.hide(); + } + + $dependent.trigger({ + type: 'state:visible', + trigger: true, + value: shouldShow, + }); + } + }); + }); + } + ); + + // Check initial state + $select.trigger('change'); + } + ); + }, + }; +})(jQuery, Drupal); diff --git a/src/Plugin/conditional_fields/handler/Select2Handler.php b/src/Plugin/conditional_fields/handler/Select2Handler.php new file mode 100644 index 0000000000000000000000000000000000000000..19ec8206c354b1fe6599a024e7796b72f10387cf --- /dev/null +++ b/src/Plugin/conditional_fields/handler/Select2Handler.php @@ -0,0 +1,59 @@ +<?php + +namespace Drupal\conditional_fields\Plugin\conditional_fields\handler; + +use Drupal\conditional_fields\ConditionalFieldsHandlerBase; +use Drupal\conditional_fields\ConditionalFieldsInterface; + +/** + * @ConditionalFieldsHandler( + * id = "states_handler_select2" + * ) + */ +class Select2Handler extends ConditionalFieldsHandlerBase { + + public function statesHandler($field, $field_info, $options) { + $state = []; + $select_states = []; + $values_array = $this->getConditionValues($options); + + // Get the proper selector using field ID instead of name + $selector = '#' . str_replace('_', '-', $field['#id']); + + switch ($options['values_set']) { + case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET: + $key_column = $field['#key_column']; + if (!empty($key_column) && isset($options['value_form'][0][$key_column])) { + $state[$options['state']] = [ + 'selector' => $selector, + 'value' => $options['value_form'][0][$key_column], + ]; + } + break; + + case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND: + $state[$options['state']] = [ + 'selector' => $selector, + 'value' => $values_array, + ]; + break; + + case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR: + $state[$options['state']] = [ + 'selector' => $selector, + 'value' => $values_array, + ]; + break; + + case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT: + $options['state'] = '!' . $options['state']; + $state[$options['state']] = [ + 'selector' => $selector, + 'value' => $values_array, + ]; + break; + } + + return $state; + } +} \ No newline at end of file