From defe18bd194f1aaec37645bb92f4f2a3a5b7dcbb Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Thu, 17 Oct 2024 00:14:33 -0400 Subject: [PATCH 1/3] Add new features (hide/show and CSS) to Modal exposed Form block A bit over-engineered, but that is what one does at 00:15 on a Thursday --- .../Block/ViewsExposedFilterBlockModal.php | 94 +++++++++++++++++-- .../filter/AdvancedSearchApiFulltext.php | 28 +++++- 2 files changed, 111 insertions(+), 11 deletions(-) diff --git a/modules/format_strawberryfield_views/src/Plugin/Block/ViewsExposedFilterBlockModal.php b/modules/format_strawberryfield_views/src/Plugin/Block/ViewsExposedFilterBlockModal.php index c04caae5..86a8a2f4 100644 --- a/modules/format_strawberryfield_views/src/Plugin/Block/ViewsExposedFilterBlockModal.php +++ b/modules/format_strawberryfield_views/src/Plugin/Block/ViewsExposedFilterBlockModal.php @@ -98,17 +98,32 @@ public function build() } } - + $advanced_search_real_id = NULL; // Hide Filters if needed foreach ($filter_ids as $field_id) { - $real_id = NULL; + $real_ids = []; if (isset($output[$field_id]) && is_array($output[$field_id])) { - $real_id = $field_id; + $real_ids[] = $field_id; } elseif (isset($output[$field_id.'_wrapper']) && is_array($output[$field_id.'_wrapper'])) { - $real_id = $field_id.'_wrapper'; + $real_ids[] = $field_id.'_wrapper'; + if (!empty($output[$field_id.'_wrapper']['#attributes']['data-advanced-wrapper'] ?? NULL)) { + // we are in the presence of a magical being. The advanced search filter. Things just got complicated. + $advanced_search_real_id = $field_id; + $count = $output[$field_id.'_advanced_search_fields_count']['#value'] ?? 1; + for ($i = 1; $i <= $count; $i++) { + if (!empty($output[$field_id.'_wrapper_'.$i]['#attributes']['data-advanced-wrapper'] ?? NULL)) { + $real_ids[] = $field_id.'_wrapper_'.$i; + } + } + } } - if ($real_id) { + //Now this gets tricky with the advanced Search. + // we might have multiple $field_id.'_wrapper_1 to N'; + // But i can do a trick here. + // Check if i have a specific data attribute. If so + + foreach ($real_ids as $real_id) { $hide = $this->checkIfNeedsToHideFilter($output[$real_id] ?? []); if ($hide) { $output[$real_id]['#attributes']['class'][] = 'visually-hidden'; @@ -186,6 +201,15 @@ public function build() $output['actions']['submit']['#attributes']['aria-hidden'] = 'true'; } + // Hide also the add more / remove if hiding advanced search + if (!$this->configuration['views_exposed_sbf_show_advanced_search_filter'] && $advanced_search_real_id) { + if (isset($output['actions'][$advanced_search_real_id . '_addone'])) { + $output['actions'][$advanced_search_real_id . '_addone']['#attributes']['class'][] = 'visually-hidden'; + $output['actions'][$advanced_search_real_id . '_addone']['#attributes']['aria-hidden'] = 'true'; + } + } + + if (!$this->configuration['views_exposed_sbf_show_reset'] && isset($output['actions']['reset'])) { $output['actions']['reset']['#attributes']['class'][] = 'visually-hidden'; $output['actions']['reset']['#attributes']['aria-hidden'] = 'true'; @@ -224,11 +248,15 @@ public function build() if (is_array($output) && !empty($output)) { $classes = $output['#attributes']['class'] ?? []; - if ($this->view->display_handler->options['defaults']['css_class']) { - $add_classes( $classes, $this->view->displayHandlers->get('default')->options['css_class']); + if (!$this->configuration['views_exposed_sbf_override_css']) { + if ($this->view->display_handler->options['defaults']['css_class']) { + $add_classes($classes, $this->view->displayHandlers->get('default')->options['css_class']); + } else { + $add_classes($classes, $this->view->display_handler->options['css_class']); + } } else { - $add_classes($classes, $this->view->display_handler->options['css_class'] ); + $add_classes($classes, trim($this->configuration['views_exposed_sbf_overriden_css'] ?? '')); } $output['#attributes']['class'] = $classes; $output += [ @@ -261,6 +289,21 @@ public function build() */ protected function checkIfNeedsToHideFilter(array $element) { $hide = FALSE; + if (isset($element['#attributes']) && is_array($element['#attributes']) + && count( + array_intersect(array_keys($element['#attributes']), ['data-advanced-search-type', 'data-advanced-wrapper'] + ) + )) { + // We return sooner just in this case, so we don't end hiding a select or any other text in case we know it is + // an Advanced Search Element, but we are not/or are hiding. + if (!$this->configuration['views_exposed_sbf_show_advanced_search_filter']) { + return TRUE; + } + else { + return FALSE; + } + } + if (!$this->configuration['views_exposed_sbf_show_text_filter'] && isset($element['#type']) && in_array( @@ -321,6 +364,9 @@ public function defaultConfiguration() { $default_config['views_exposed_sbf_rename_submit_label'] = ''; $default_config['views_exposed_sbf_copy_values_to_other_js'] = 0; $default_config['views_exposed_sbf_autosubmit_js'] = 0; + $default_config['views_exposed_sbf_show_advanced_search_filter'] = 0; + $default_config['views_exposed_sbf_override_css'] = 0; + $default_config['views_exposed_sbf_overriden_css'] = ''; return $default_config; } @@ -346,13 +392,15 @@ public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['views_exposed_sbf_show_text_filter'] = $form_state->getValue('views_exposed_sbf_show_text_filter', 0) ; $this->configuration['views_exposed_sbf_show_select_filter'] = $form_state->getValue('views_exposed_sbf_show_select_filter', 0) ; $this->configuration['views_exposed_sbf_show_checksandoptions_filter'] = $form_state->getValue('views_exposed_sbf_show_checksandoptions_filter',0); + $this->configuration['views_exposed_sbf_show_advanced_search_filter'] = $form_state->getValue('views_exposed_sbf_show_advanced_search_filter',0); $this->configuration['views_exposed_sbf_show_sort_filter'] = $form_state->getValue('views_exposed_sbf_show_sort_filter',0); $this->configuration['views_exposed_sbf_show_pager'] = $form_state->getValue('views_exposed_sbf_show_pager',0); $this->configuration['views_exposed_sbf_show_submit'] = $form_state->getValue('views_exposed_sbf_show_submit',0); $this->configuration['views_exposed_sbf_show_reset'] = $form_state->getValue('views_exposed_sbf_show_reset',0); $this->configuration['views_exposed_sbf_copy_values_to_other_js'] = $form_state->getValue('views_exposed_sbf_copy_values_to_other_js',0); $this->configuration['views_exposed_sbf_autosubmit_js'] = $form_state->getValue('views_exposed_sbf_autosubmit_js',0); - + $this->configuration['views_exposed_sbf_override_css'] = $form_state->getValue('views_exposed_sbf_override_css',0); + $this->configuration['views_exposed_sbf_overriden_css'] = $form_state->getValue('views_exposed_sbf_override_css',0) ? $form_state->getValue('views_exposed_sbf_override_css','') : ''; $form_state->unsetValue('views_label_checkbox'); $form_state->unsetValue('views_exposed_sbf_rename_submit'); } @@ -368,6 +416,13 @@ public function buildConfigurationForm(array $form, '#title' => 'Exposed Form element and component Visibility' ]; + $form['views_exposed_sbf_show_advanced_search_filter'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Show Advanced Search Filter if exposed'), + '#description' => $this->t('Disabling Select and Text individually will have no effect on the Complex Advanced Search Filter and its internal components of those types'), + '#default_value' => !empty($this->configuration['views_exposed_sbf_show_advanced_search_filter']), + '#fieldset' => 'views_exposed_sbf_fieldset', + ]; $form['views_exposed_sbf_show_text_filter'] = [ '#type' => 'checkbox', '#title' => $this->t('Show filter components of type textfield if exposed'), @@ -452,7 +507,7 @@ public function buildConfigurationForm(array $form, ]; $form['views_exposed_sbf_autosubmit_js'] = [ '#type' => 'checkbox', - '#title' => $this->t('Auto submits the form, on user interaction/change of select, checkboxes and option elements. '), + '#title' => $this->t('Auto submits the form, on user interaction/change of select, checkboxes and option elements. This excludes Advanced Search and its internal components. '), '#default_value' => !empty($this->configuration['views_exposed_sbf_autosubmit_js']), '#fieldset' => 'views_exposed_sbf_fieldset', '#states' => [ @@ -483,6 +538,25 @@ public function buildConfigurationForm(array $form, ], '#fieldset' => 'views_exposed_sbf_fieldset', ]; + $form['views_exposed_sbf_override_css'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Override CSS inherited from the View '), + '#default_value' => !empty($this->configuration['views_exposed_sbf_override_css']), + '#fieldset' => 'views_exposed_sbf_fieldset', + ]; + $form['views_exposed_sbf_overriden_css'] = [ + '#title' => $this->t('CSS classes separated by spaces'), + '#type' => 'textfield', + '#default_value' => $this->configuration['views_exposed_sbf_overriden_css'] ?: '', + '#states' => [ + 'visible' => [ + [ + ':input[name="settings[views_exposed_sbf_override_css]"]' => ['checked' => TRUE], + ], + ], + ], + '#fieldset' => 'views_exposed_sbf_fieldset', + ]; return $form; } diff --git a/modules/format_strawberryfield_views/src/Plugin/views/filter/AdvancedSearchApiFulltext.php b/modules/format_strawberryfield_views/src/Plugin/views/filter/AdvancedSearchApiFulltext.php index e090d9ef..5b2c0e15 100644 --- a/modules/format_strawberryfield_views/src/Plugin/views/filter/AdvancedSearchApiFulltext.php +++ b/modules/format_strawberryfield_views/src/Plugin/views/filter/AdvancedSearchApiFulltext.php @@ -646,6 +646,9 @@ protected function valueForm(&$form, FormStateInterface $form_state) { '#title' => !$form_state->get('exposed') ? $this->t('Value') : '', '#size' => 30, '#default_value' => $this->value[$this->options['expose']['identifier']] ?? '', + '#context' => [ + '#filter_type' => 'sbf_advanced_search' + ], ]; if (!empty($this->options['expose']['placeholder'])) { $form['value']['#attributes']['placeholder'] = $this->options['expose']['placeholder']; @@ -722,6 +725,11 @@ public function buildExposedForm(&$form, FormStateInterface $form_state) { $searched_fields_identifier = $this->options['expose']['searched_fields_id']; } + $inter_field_op_fields_identifier = $this->options['id'] . '_op'; + if (!empty($this->options['expose']['operator_id'])) { + $inter_field_op_fields_identifier + = $this->options['expose']['operator_id']; + } // Remove the group operator if found unset($form[$searched_fields_identifier]); @@ -733,6 +741,9 @@ public function buildExposedForm(&$form, FormStateInterface $form_state) { '#multiple' => $this->options['expose']['multiple'] ?? FALSE, '#size' => $multiple_exposed_fields, '#default_value' => $form_state->getValue($searched_fields_identifier), + '#context' => [ + '#filter_type' => 'sbf_advanced_search' + ], '#attributes' => [ 'data-advanced-search-type' => 'fields' ] @@ -745,6 +756,9 @@ public function buildExposedForm(&$form, FormStateInterface $form_state) { } $form[$this->options['id'] . '_wrapper'][$searched_fields_identifier] = $newelements[$searched_fields_identifier]; + if (isset($form[$this->options['id'] . '_wrapper'][$inter_field_op_fields_identifier])) { + $form[$this->options['id'] . '_wrapper'][$inter_field_op_fields_identifier]['#attributes']['data-advanced-search-type'] = 'boolean'; + } } $advanced_search_operator_id = $this->options['id'] . '_group_operator'; @@ -769,6 +783,9 @@ public function buildExposedForm(&$form, FormStateInterface $form_state) { '#default_value' => $form_state->getValue($advanced_search_operator_id,'or'), '#attributes' => [ 'data-advanced-search-type' => 'op' + ], + '#context' => [ + '#filter_type' => 'sbf_advanced_search' ] ]; $form[$this->options['id'] . '_wrapper'][$advanced_search_operator_id] = $newelements[$advanced_search_operator_id]; @@ -813,6 +830,9 @@ public function buildExposedForm(&$form, FormStateInterface $form_state) { '#access' => $enable_more, '#weight' => '-100', '#group' => 'actions', + '#context' => [ + '#filter_type' => 'sbf_advanced_search' + ], ]; // If classic mode, hide instead of disabling. The form is still validated, so people even if they tru to // trick the JS, won't be able to process more or less than we have defined in the settings. @@ -847,6 +867,9 @@ public function buildExposedForm(&$form, FormStateInterface $form_state) { '#access' => $enable_less, '#weight' => '-101', '#group' => 'actions', + '#context' => [ + '#filter_type' => 'sbf_advanced_search' + ], ]; } // If classic mode, hide instead of disabling. The form is still validated, so people even if they tru to @@ -940,8 +963,11 @@ public function buildExposedForm(&$form, FormStateInterface $form_state) { $form[$this->options['id'].'_wrapper_'.$i]['#title_display'] = 'invisible'; } } + // Adds also the data attribute but with a different value to the main/initial wrapper. + if (isset($form[$this->options['id'].'_wrapper'])) { + $form[$this->options['id'].'_wrapper']['#attributes']['data-advanced-wrapper'] = "main"; + } - $form = $form; } From 16c25a03b98fa7c579ec5e65cf862968836d115d Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Thu, 17 Oct 2024 00:14:59 -0400 Subject: [PATCH 2/3] Improves JS/autosubmit to ignore Advanced Search --- .../js/modal-exposed-form-interactions.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/format_strawberryfield_views/js/modal-exposed-form-interactions.js b/modules/format_strawberryfield_views/js/modal-exposed-form-interactions.js index 563f9df6..b66a0e84 100644 --- a/modules/format_strawberryfield_views/js/modal-exposed-form-interactions.js +++ b/modules/format_strawberryfield_views/js/modal-exposed-form-interactions.js @@ -27,8 +27,10 @@ } // Note that hidden is allowed bc there might be cases where we override Selects to use Bootstrap components. So the // original value/and trigger is the hidden element after being set by the component. - const allowed_autosubmit_types = ['select', 'checkbox', 'radio', 'hidden']; - if (allowed_autosubmit_types.includes(event.target.type)) { + const allowed_autosubmit_types = ['select', 'select-one', 'checkbox', 'radio', 'hidden']; + // Don't allow Advanced Search to trigger anything. + + if (allowed_autosubmit_types.includes(event.target.type) && typeof event.target.dataset?.advancedSearchType == "undefined") { const autosubmit_form = event.target.closest('div[data-sbf-modalblock-autosubmit=true] form'); if (autosubmit_form) { autosubmit_form.submit(); @@ -40,7 +42,7 @@ input.addEventListener("change", this.onChangeHandler); //const $combined_view = $input.data('drupal-target-view'); } - if ($context.is('div[ata-sbf-modalblock-copytothers=true], div[data-sbf-modalblock-autosubmit=true] form')) { + if ($context.is('div[data-sbf-modalblock-copytothers=true], div[data-sbf-modalblock-autosubmit=true] form')) { var $that = this; once('modal-block-form', 'form', context).forEach(function (value, index) { $that.attachModalFormInteractions(value); From c7245c7800022a2b7345344f6f061bc7d7872628 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Thu, 17 Oct 2024 00:22:45 -0400 Subject: [PATCH 3/3] Wrong config key for css --- .../src/Plugin/Block/ViewsExposedFilterBlockModal.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/format_strawberryfield_views/src/Plugin/Block/ViewsExposedFilterBlockModal.php b/modules/format_strawberryfield_views/src/Plugin/Block/ViewsExposedFilterBlockModal.php index 86a8a2f4..4ff8127a 100644 --- a/modules/format_strawberryfield_views/src/Plugin/Block/ViewsExposedFilterBlockModal.php +++ b/modules/format_strawberryfield_views/src/Plugin/Block/ViewsExposedFilterBlockModal.php @@ -400,7 +400,7 @@ public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['views_exposed_sbf_copy_values_to_other_js'] = $form_state->getValue('views_exposed_sbf_copy_values_to_other_js',0); $this->configuration['views_exposed_sbf_autosubmit_js'] = $form_state->getValue('views_exposed_sbf_autosubmit_js',0); $this->configuration['views_exposed_sbf_override_css'] = $form_state->getValue('views_exposed_sbf_override_css',0); - $this->configuration['views_exposed_sbf_overriden_css'] = $form_state->getValue('views_exposed_sbf_override_css',0) ? $form_state->getValue('views_exposed_sbf_override_css','') : ''; + $this->configuration['views_exposed_sbf_overriden_css'] = $form_state->getValue('views_exposed_sbf_override_css', 0) ? $form_state->getValue('views_exposed_sbf_overriden_css','') : ''; $form_state->unsetValue('views_label_checkbox'); $form_state->unsetValue('views_exposed_sbf_rename_submit'); }