Skip to content

Commit

Permalink
Merge pull request #477 from esmero/ISSUE-476
Browse files Browse the repository at this point in the history
ISSUE-476: Advanced Search and Modal Exposed Form
  • Loading branch information
DiegoPino authored Oct 17, 2024
2 parents 4d2c7d3 + c7245c7 commit 728535f
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 += [
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand All @@ -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_overriden_css','') : '';
$form_state->unsetValue('views_label_checkbox');
$form_state->unsetValue('views_exposed_sbf_rename_submit');
}
Expand All @@ -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'),
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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]);
Expand All @@ -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'
]
Expand All @@ -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';
Expand All @@ -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];
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}


Expand Down

0 comments on commit 728535f

Please sign in to comment.