Skip to content

Commit

Permalink
EWPP-3209: Namespacing in the form storage the location where the def…
Browse files Browse the repository at this point in the history
…ault and contextual filter values go.
  • Loading branch information
upchuk committed Sep 11, 2023
1 parent 5ae69e1 commit 08fdf73
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public function buildContextualFilters(array $form, FormStateInterface $form_sta
'#value' => $current_filters,
];

$facet_id = $form_state->get('contextual_facet_id');
$facet_id = $form_state->get('contextual_facet_id_' . $ajax_wrapper_id);

// If we could not determine a facet ID, we default to showing the summary
// of default values.
if (!$facet_id) {
return $this->buildSummaryPresetFilters($form, $form_state, $list_source, $this->getAvailableFilters($list_source));
}

$filter_id = $form_state->get('contextual_filter_id');
$filter_id = $form_state->get('contextual_filter_id_' . $ajax_wrapper_id);
if (!isset($filter_id)) {
$filter_id = static::generateFilterId($facet_id, array_keys($current_filters));
}
Expand Down Expand Up @@ -152,7 +152,7 @@ protected function buildEditContextualFilter(array $form, FormStateInterface $fo

// Store the filter IDs on the form state in case we need to rebuild the
// form.
$form_state->set(static::getFilterType() . '_filter_id', $filter_id);
$form_state->set(static::getFilterType() . '_filter_id_' . $ajax_wrapper_id, $filter_id);

$facet = $this->getFacetById($list_source, $facet_id);
if (!empty($facet) && ($widget = $facet->getWidgetInstance()) && ($widget instanceof MultiselectWidget)) {
Expand Down Expand Up @@ -202,13 +202,14 @@ protected function buildEditContextualFilter(array $form, FormStateInterface $fo
'#facet_id' => $facet_id,
'#executes_submit_callback' => TRUE,
'#submit' => [[$this, 'setContextualOptionsSubmit']],
'#name' => static::getFilterType() . '-set-' . $filter_id . '-' . $ajax_wrapper_id,
];

$form['wrapper']['edit'][$filter_id]['cancel_value'] = [
'#value' => $this->t('Cancel'),
'#type' => 'button',
'#op' => 'cancel-contextual-filter',
'#name' => static::getFilterType() . '-cancel-' . $filter_id,
'#name' => static::getFilterType() . '-cancel-' . $filter_id . '-' . $ajax_wrapper_id,
'#limit_validation_errors' => [
array_merge($form['#parents'], [
'wrapper',
Expand Down Expand Up @@ -309,8 +310,8 @@ public function setContextualOptionsSubmit(array &$form, FormStateInterface $for

// Set the current filters on the form state so they can be used elsewhere.
static::setCurrentValues($form_state, $list_source, $current_filters, $ajax_wrapper_id);
$form_state->set('contextual_facet_id', NULL);
$form_state->set('contextual_filter_id', NULL);
$form_state->set('contextual_facet_id_' . $ajax_wrapper_id, NULL);
$form_state->set('contextual_filter_id_' . $ajax_wrapper_id, NULL);
$form_state->setRebuild(TRUE);
}

Expand All @@ -334,8 +335,8 @@ public function deleteFilterValueSubmit(array &$form, FormStateInterface $form_s

unset($current_filters[$filter_id]);
static::setCurrentValues($form_state, $list_source, $current_filters, $ajax_wrapper_id);
$form_state->set('contextual_facet_id', NULL);
$form_state->set('contextual_filter_id', NULL);
$form_state->set('contextual_facet_id_' . $ajax_wrapper_id, NULL);
$form_state->set('contextual_filter_id_' . $ajax_wrapper_id, NULL);

$form_state->setRebuild(TRUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testListPagePresetFilters(): void {
$this->assertSession()->assertWaitOnAjaxRequest();
$this->getSession()->getPage()->selectFieldOption('No results behaviour', 'Hide');
$this->assertSession()->assertWaitOnAjaxRequest();
$this->assertListPagePresetFilters('configuration[0][link_source][plugin_configuration_wrapper][list_pages][list_page_configuration][wrapper][default_filter_values]');
$this->assertListPagePresetFilters('configuration[0][link_source][plugin_configuration_wrapper][list_pages][list_page_configuration][wrapper][default_filter_values]', 'list-page-default_filter_values--configuration-0-link_source-plugin_configuration_wrapper-list_pages-list_page_configuration-wrapper-default_filter_values');
}

/**
Expand Down Expand Up @@ -317,6 +317,8 @@ public function testMultipleListPageFilters(): void {
public function testListPageContextualFiltersForm(): void {
$contextual_filter_name_prefix = 'configuration[0][link_source][plugin_configuration_wrapper][list_pages][list_page_configuration][wrapper][contextual_filters]';
$default_value_name_prefix = 'configuration[0][link_source][plugin_configuration_wrapper][list_pages][list_page_configuration][wrapper][default_filter_values]';
$ajax_wrapper_id_default_filter = 'list-page-default_filter_values--configuration-0-link_source-plugin_configuration_wrapper-list_pages-list_page_configuration-wrapper-default_filter_values';
$ajax_wrapper_id_contextual_filter = 'list-page-contextual-filter-values--configuration-0-link_source-plugin_configuration_wrapper-list_pages-list_page_configuration-wrapper-contextual_filters';

$admin = $this->createUser([], NULL, TRUE);
$this->drupalLogin($admin);
Expand Down Expand Up @@ -394,7 +396,7 @@ public function testListPageContextualFiltersForm(): void {
$this->assertContextualValueForFilters($expected_contextual_filters);

// Edit the Reference.
$page->pressButton('contextual-edit-' . $reference_filter_id);
$page->pressButton('contextual-edit-' . $reference_filter_id . '-' . $ajax_wrapper_id_contextual_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextContains('Set contextual options for Reference');
$this->assertTrue($this->assertSession()->optionExists('Operator', 'All of')->hasAttribute('selected'));
Expand Down Expand Up @@ -426,40 +428,40 @@ public function testListPageContextualFiltersForm(): void {

// Start editing one contextual filter and one default value at the same
// time.
$page->pressButton('contextual-edit-' . $link_filter_id);
$page->pressButton('contextual-edit-' . $link_filter_id . '-' . $ajax_wrapper_id_contextual_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextContains('Set contextual options for Link');
$this->assertTrue($this->assertSession()->optionExists('Operator', 'None of')->hasAttribute('selected'));

$page->pressButton('default-edit-' . $body_filter_id);
$page->pressButton('default-edit-' . $body_filter_id . '-' . $ajax_wrapper_id_default_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextContains('Set default value for Body');
$filter_selector = $default_value_name_prefix . '[wrapper][edit][' . $body_filter_id . '][body]';
$this->assertSession()->fieldValueEquals($filter_selector, 'cherry');

// Cancel out of both forms, one at the time.
$page->pressButton('contextual-cancel-' . $link_filter_id);
$page->pressButton('contextual-cancel-' . $link_filter_id . '-' . $ajax_wrapper_id_contextual_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextNotContains('Set contextual options for Link');
$this->assertContextualValueForFilters($expected_contextual_filters);
$assert->pageTextContains('Set default value for Body');
$filter_selector = $default_value_name_prefix . '[wrapper][edit][' . $body_filter_id . '][body]';
$this->assertSession()->fieldValueEquals($filter_selector, 'cherry');

$page->pressButton('default-cancel-' . $body_filter_id);
$page->pressButton('default-cancel-' . $body_filter_id . '-' . $ajax_wrapper_id_default_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextNotContains('Set default value for Body');
$this->assertContextualValueForFilters($expected_contextual_filters);
$this->assertDefaultValueForFilters($expected_default_filters);

// Edit again one contextual filter and one default value at the same
// time.
$page->pressButton('contextual-edit-' . $reference_filter_id);
$page->pressButton('contextual-edit-' . $reference_filter_id . '-' . $ajax_wrapper_id_contextual_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextContains('Set contextual options for Reference');
$this->assertTrue($this->assertSession()->optionExists('Operator', 'None of')->hasAttribute('selected'));

$page->pressButton('default-edit-' . $body_filter_id);
$page->pressButton('default-edit-' . $body_filter_id . '-' . $ajax_wrapper_id_default_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextContains('Set default value for Body');
$filter_selector = $default_value_name_prefix . '[wrapper][edit][' . $body_filter_id . '][body]';
Expand Down Expand Up @@ -530,21 +532,21 @@ public function testListPageContextualFiltersForm(): void {
$this->clickLink('Edit');
$this->assertContextualValueForFilters($expected_contextual_filters);
$this->assertDefaultValueForFilters($expected_default_filters);
$page->pressButton('contextual-edit-' . $link_filter_id);
$page->pressButton('contextual-edit-' . $link_filter_id . '-' . $ajax_wrapper_id_contextual_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextContains('Set contextual options for Link');
$this->assertTrue($this->assertSession()->optionExists('Operator', 'None of')->hasAttribute('selected'));
$this->assertTrue($this->assertSession()->optionExists('Filter source', 'Field values')->hasAttribute('selected'));
$page->pressButton('contextual-cancel-' . $link_filter_id);
$page->pressButton('contextual-cancel-' . $link_filter_id . '-' . $ajax_wrapper_id_contextual_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$this->assertContextualValueForFilters($expected_contextual_filters);
$page->pressButton('contextual-edit-' . $reference_filter_id);
$page->pressButton('contextual-edit-' . $reference_filter_id . '-' . $ajax_wrapper_id_contextual_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextContains('Set contextual options for Reference');
$this->assertTrue($this->assertSession()->optionExists('Operator', 'Any of')->hasAttribute('selected'));
$this->assertTrue($this->assertSession()->optionExists('Filter source', 'Entity ID')->hasAttribute('selected'));

$page->pressButton('default-edit-' . $body_filter_id);
$page->pressButton('default-edit-' . $body_filter_id . '-' . $ajax_wrapper_id_default_filter);
$this->assertSession()->assertWaitOnAjaxRequest();
$assert->pageTextContains('Set default value for Body');
$filter_selector = $default_value_name_prefix . '[wrapper][edit][' . $body_filter_id . '][body]';
Expand Down
21 changes: 11 additions & 10 deletions src/DefaultFilterConfigurationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function buildDefaultFilters(array $form, FormStateInterface $form_state,
'#value' => $current_filters,
];

$facet_id = $form_state->get('default_facet_id');
$facet_id = $form_state->get('default_facet_id_' . $ajax_wrapper_id);

// If we could not determine a facet ID, we default to showing the
// summary of default values.
if (!$facet_id) {
return $this->buildSummaryPresetFilters($form, $form_state, $list_source, $list_source->getAvailableFilters());
}

$filter_id = $form_state->get('default_filter_id');
$filter_id = $form_state->get('default_filter_id_' . $ajax_wrapper_id);
if (!isset($filter_id)) {
$filter_id = static::generateFilterId($facet_id, array_keys($current_filters));
}
Expand Down Expand Up @@ -119,7 +119,7 @@ protected function buildEditPresetFilter(array $form, FormStateInterface $form_s

// Store the filter IDs on the form state in case we need to rebuild the
// form.
$form_state->set(static::getFilterType() . '_filter_id', $filter_id);
$form_state->set(static::getFilterType() . '_filter_id_' . $ajax_wrapper_id, $filter_id);

$facet = $this->getFacetById($list_source, $facet_id);
if (!empty($facet) && ($widget = $facet->getWidgetInstance()) && ($widget instanceof ListPagesWidgetInterface)) {
Expand Down Expand Up @@ -151,6 +151,7 @@ protected function buildEditPresetFilter(array $form, FormStateInterface $form_s
'#limit_validation_errors' => [
array_merge($form['#parents'], ['wrapper', 'edit']),
],
'#name' => static::getFilterType() . '-set-' . $filter_id . '-' . $ajax_wrapper_id,
'#ajax' => $ajax_definition,
'#filter_id' => $filter_id,
'#facet_id' => $facet_id,
Expand All @@ -161,7 +162,7 @@ protected function buildEditPresetFilter(array $form, FormStateInterface $form_s
$form['wrapper']['edit'][$filter_id]['cancel_value'] = [
'#value' => $this->t('Cancel'),
'#type' => 'button',
'#name' => static::getFilterType() . '-cancel-' . $filter_id,
'#name' => static::getFilterType() . '-cancel-' . $filter_id . '-' . $ajax_wrapper_id,
'#limit_validation_errors' => [
array_merge($form['#parents'], [
'wrapper',
Expand Down Expand Up @@ -224,9 +225,9 @@ public function setDefaultValueSubmit(array &$form, FormStateInterface $form_sta

// Set the current filters on the form state so they can be used elsewhere.
static::setCurrentValues($form_state, $list_source, $current_filters, $ajax_wrapper_id);
$form_state->set('default_facet_id', NULL);
$form_state->set('default_filter_id', NULL);
$form_state->set('default_filter_storage', NULL);
$form_state->set('default_facet_id_' . $ajax_wrapper_id, NULL);
$form_state->set('default_filter_id_' . $ajax_wrapper_id, NULL);
$form_state->set('default_filter_storage_' . $ajax_wrapper_id, NULL);
$form_state->setRebuild(TRUE);
}

Expand Down Expand Up @@ -285,10 +286,10 @@ public function deleteFilterValueSubmit(array &$form, FormStateInterface $form_s
$widget->prepareDefaultFilterValue($facet, $current_filters[$filter_id], $subform_state);
unset($current_filters[$filter_id]);
static::setCurrentValues($form_state, $list_source, $current_filters, $ajax_wrapper_id);
$form_state->set('default_filter_id', NULL);
$form_state->set('default_filter_id_' . $ajax_wrapper_id, NULL);
// Clear also the storage that plugins may use for this filter.
$form_state->set('default_filter_storage', NULL);
$form_state->set('default_facet_id', NULL);
$form_state->set('default_filter_storage_' . $ajax_wrapper_id, NULL);
$form_state->set('default_facet_id_' . $ajax_wrapper_id, NULL);

$form_state->setRebuild(TRUE);
}
Expand Down
25 changes: 16 additions & 9 deletions src/FilterConfigurationFormBuilderBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ protected function buildSummaryPresetFilters(array $form, FormStateInterface $fo
$form['wrapper']['buttons'][$filter_id]['edit-' . $filter_id] = [
'#type' => 'button',
'#value' => $this->t('Edit'),
'#name' => static::getFilterType() . '-edit-' . $filter_id,
'#name' => static::getFilterType() . '-edit-' . $filter_id . '-' . $ajax_wrapper_id,
'#filter_id' => $filter_id,
'#facet_id' => $filter->getFacetId(),
'#limit_validation_errors' => [
array_merge($form['#parents'], [
'wrapper',
'edit',
$filter_id,
static::getFilterType() . '-edit-' . $filter_id,
static::getFilterType() . '-edit-' . $filter_id . '-' . $ajax_wrapper_id,
]),
],
'#ajax' => [
Expand All @@ -174,15 +174,15 @@ protected function buildSummaryPresetFilters(array $form, FormStateInterface $fo
$form['wrapper']['buttons'][$filter_id]['delete-' . $filter_id] = [
'#type' => 'button',
'#value' => $this->t('Delete'),
'#name' => static::getFilterType() . '-delete-' . $filter_id,
'#name' => static::getFilterType() . '-delete-' . $filter_id . '-' . $ajax_wrapper_id,
'#filter_id' => $filter_id,
'#facet_id' => $filter->getFacetId(),
'#limit_validation_errors' => [
array_merge($form['#parents'], [
'wrapper',
'edit',
$filter_id,
static::getFilterType() . '-delete-' . $filter_id,
static::getFilterType() . '-delete-' . $filter_id . '-' . $ajax_wrapper_id,
]),
],
'#ajax' => [
Expand Down Expand Up @@ -293,8 +293,10 @@ public static function generateFilterId(string $id, array $existing_filters = []
*/
public function addFacetSubmit(array &$form, FormStateInterface $form_state): void {
$triggering_element = $form_state->getTriggeringElement();
$element = NestedArray::getValue($form, array_slice($triggering_element['#array_parents'], 0, -3));
$ajax_wrapper_id = $this->getAjaxWrapperId($element);
$filter_type = static::getFilterType();
$key = $filter_type . '_facet_id';
$key = $filter_type . '_facet_id_' . $ajax_wrapper_id;
$form_state->set($key, $triggering_element['#value']);
$form_state->setRebuild(TRUE);
}
Expand Down Expand Up @@ -343,8 +345,10 @@ public function refreshEditFormAjax(array &$form, FormStateInterface $form_state
*/
public function editFilterSubmit(array &$form, FormStateInterface $form_state): void {
$triggering_element = $form_state->getTriggeringElement();
$form_state->set(static::getFilterType() . '_facet_id', $triggering_element['#facet_id']);
$form_state->set(static::getFilterType() . '_filter_id', $triggering_element['#filter_id']);
$element = NestedArray::getValue($form, array_slice($triggering_element['#array_parents'], 0, -4));
$ajax_wrapper_id = $this->getAjaxWrapperId($element);
$form_state->set(static::getFilterType() . '_facet_id_' . $ajax_wrapper_id, $triggering_element['#facet_id']);
$form_state->set(static::getFilterType() . '_filter_id_' . $ajax_wrapper_id, $triggering_element['#filter_id']);
$form_state->setRebuild(TRUE);
}

Expand All @@ -357,8 +361,11 @@ public function editFilterSubmit(array &$form, FormStateInterface $form_state):
* The form state.
*/
public function cancelValueSubmit(array &$form, FormStateInterface $form_state): void {
$form_state->set(static::getFilterType() . '_facet_id', NULL);
$form_state->set(static::getFilterType() . '_filter_id', NULL);
$triggering_element = $form_state->getTriggeringElement();
$element = NestedArray::getValue($form, array_slice($triggering_element['#array_parents'], 0, -4));
$ajax_wrapper_id = $this->getAjaxWrapperId($element);
$form_state->set(static::getFilterType() . '_facet_id_' . $ajax_wrapper_id, NULL);
$form_state->set(static::getFilterType() . '_filter_id_' . $ajax_wrapper_id, NULL);
// Clear also any filter specific storage information plugins may set.
$form_state->set(static::getFilterType() . '_filter_storage', NULL);
$form_state->setRebuild(TRUE);
Expand Down
Loading

0 comments on commit 08fdf73

Please sign in to comment.