Skip to content

Commit

Permalink
Merge pull request #178 from openeuropa/EWPP-3209
Browse files Browse the repository at this point in the history
EWPP-3209: Fixing form storage for default and contextual filters.
  • Loading branch information
upchuk authored Sep 11, 2023
2 parents b43ed24 + 08fdf73 commit 8a7f2a2
Show file tree
Hide file tree
Showing 12 changed files with 509 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ public function buildContextualFilters(array $form, FormStateInterface $form_sta
'#type' => 'label',
];

$this->initializeCurrentContextualFilterValues($form_state, $configuration, $list_source);
$current_filters = static::getCurrentValues($form_state, $list_source);
$this->initializeCurrentContextualFilterValues($form, $form_state, $configuration, $list_source);
$current_filters = static::getCurrentValues($form_state, $list_source, $ajax_wrapper_id);
// Set the current filters on the form so they can be used in the submit.
$form['current_filters'] = [
'#type' => 'value',
'#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 All @@ -93,7 +93,7 @@ public function buildContextualFilters(array $form, FormStateInterface $form_sta
protected function buildSummaryPresetFilters(array $form, FormStateInterface $form_state, ListSourceInterface $list_source, array $available_filters = []): array {
$form = parent::buildSummaryPresetFilters($form, $form_state, $list_source, $available_filters);
/** @var \Drupal\oe_list_pages_link_list_source\ContextualPresetFilter[] $current_filters */
$current_filters = static::getCurrentValues($form_state, $list_source);
$current_filters = static::getCurrentValues($form_state, $list_source, $this->getAjaxWrapperId($form));

$form['wrapper']['summary']['table']['#header'][1]['data'] = $this->t('Operator');
foreach ($form['wrapper']['summary']['table']['#rows'] as $key => &$row) {
Expand Down Expand Up @@ -142,7 +142,7 @@ protected function buildEditContextualFilter(array $form, FormStateInterface $fo
$ajax_wrapper_id = $this->getAjaxWrapperId($form);

/** @var \Drupal\oe_list_pages_link_list_source\ContextualPresetFilter[] $current_filters */
$current_filters = static::getCurrentValues($form_state, $list_source);
$current_filters = static::getCurrentValues($form_state, $list_source, $ajax_wrapper_id);
$available_filters = $this->getAvailableFilters($list_source);

$form['wrapper']['edit'] = [
Expand All @@ -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 All @@ -229,17 +230,20 @@ protected function buildEditContextualFilter(array $form, FormStateInterface $fo
/**
* Initialize the form state with the values of the current list source.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
* @param array $configuration
* The current plugin configuration.
* @param \Drupal\oe_list_pages\ListSourceInterface $list_source
* The list source.
*/
protected function initializeCurrentContextualFilterValues(FormStateInterface $form_state, array $configuration, ListSourceInterface $list_source): void {
protected function initializeCurrentContextualFilterValues(array $form, FormStateInterface $form_state, array $configuration, ListSourceInterface $list_source): void {
// If we have current values for this list source, we can keep them going
// forward.
$values = static::getCurrentValues($form_state, $list_source);
$ajax_wrapper_id = $this->getAjaxWrapperId($form);
$values = static::getCurrentValues($form_state, $list_source, $ajax_wrapper_id);
if ($values) {
return;
}
Expand All @@ -248,13 +252,13 @@ protected function initializeCurrentContextualFilterValues(FormStateInterface $f
// passed configuration and set the ones from the configuration if they do.
// We also check if the values have not been emptied in the current
// "session".
if ($list_source->getEntityType() === $configuration['entity_type'] && $list_source->getBundle() === $configuration['bundle'] && !static::areCurrentValuesEmpty($form_state, $list_source)) {
if ($list_source->getEntityType() === $configuration['entity_type'] && $list_source->getBundle() === $configuration['bundle'] && !static::areCurrentValuesEmpty($form_state, $list_source, $ajax_wrapper_id)) {
$values = $configuration['contextual_filters'] ?? [];
static::setCurrentValues($form_state, $list_source, $values);
static::setCurrentValues($form_state, $list_source, $values, $ajax_wrapper_id);
return;
}

static::setCurrentValues($form_state, $list_source, []);
static::setCurrentValues($form_state, $list_source, [], $ajax_wrapper_id);
}

/**
Expand Down Expand Up @@ -285,8 +289,11 @@ public function setContextualOptionsAjax(array &$form, FormStateInterface $form_
public function setContextualOptionsSubmit(array &$form, FormStateInterface $form_state): void {
/** @var \Drupal\oe_list_pages\ListSourceInterface $list_source */
$list_source = $form_state->get('list_source');
$current_filters = static::getCurrentValues($form_state, $list_source);
$triggering_element = $form_state->getTriggeringElement();
$element = NestedArray::getValue($form, array_slice($triggering_element['#array_parents'], 0, -4));
$ajax_wrapper_id = $this->getAjaxWrapperId($element);
$current_filters = static::getCurrentValues($form_state, $list_source, $ajax_wrapper_id);

$facet_id = $triggering_element['#facet_id'];
$filter_id = $triggering_element['#filter_id'];

Expand All @@ -302,9 +309,9 @@ public function setContextualOptionsSubmit(array &$form, FormStateInterface $for
$current_filters[$filter_id]->setFilterSource($filter_source);

// Set the current filters on the form state so they can be used elsewhere.
static::setCurrentValues($form_state, $list_source, $current_filters);
$form_state->set('contextual_facet_id', NULL);
$form_state->set('contextual_filter_id', NULL);
static::setCurrentValues($form_state, $list_source, $current_filters, $ajax_wrapper_id);
$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 @@ -314,8 +321,11 @@ public function setContextualOptionsSubmit(array &$form, FormStateInterface $for
public function deleteFilterValueSubmit(array &$form, FormStateInterface $form_state): void {
/** @var \Drupal\oe_list_pages\ListSourceInterface $list_source */
$list_source = $form_state->get('list_source');
$current_filters = static::getCurrentValues($form_state, $list_source);
$triggering_element = $form_state->getTriggeringElement();
$element = NestedArray::getValue($form, array_slice($triggering_element['#array_parents'], 0, -4));
$ajax_wrapper_id = $this->getAjaxWrapperId($element);
$current_filters = static::getCurrentValues($form_state, $list_source, $ajax_wrapper_id);

$facet_id = $triggering_element['#facet_id'];
$filter_id = $triggering_element['#filter_id'];

Expand All @@ -324,9 +334,9 @@ public function deleteFilterValueSubmit(array &$form, FormStateInterface $form_s
}

unset($current_filters[$filter_id]);
static::setCurrentValues($form_state, $list_source, $current_filters);
$form_state->set('contextual_facet_id', NULL);
$form_state->set('contextual_filter_id', NULL);
static::setCurrentValues($form_state, $list_source, $current_filters, $ajax_wrapper_id);
$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
@@ -0,0 +1,77 @@
langcode: en
status: true
dependencies:
config:
- field.field.node.content_type_three.field_link_lists
- node.type.content_type_three
module:
- inline_entity_form
id: node.content_type_three.default
targetEntityType: node
bundle: content_type_three
mode: default
content:
created:
type: datetime_timestamp
weight: 10
region: content
settings: { }
third_party_settings: { }
field_link_lists:
type: inline_entity_form_complex
weight: 121
region: content
settings:
form_mode: default
override_labels: false
label_singular: ''
label_plural: ''
allow_new: true
allow_existing: false
match_operator: CONTAINS
allow_duplicate: false
collapsible: false
collapsed: false
revision: false
removed_reference: optional
third_party_settings: { }
promote:
type: boolean_checkbox
weight: 15
region: content
settings:
display_label: true
third_party_settings: { }
status:
type: boolean_checkbox
weight: 120
region: content
settings:
display_label: true
third_party_settings: { }
sticky:
type: boolean_checkbox
weight: 16
region: content
settings:
display_label: true
third_party_settings: { }
title:
type: string_textfield
weight: -5
region: content
settings:
size: 60
placeholder: ''
third_party_settings: { }
uid:
type: entity_reference_autocomplete
weight: 5
region: content
settings:
match_operator: CONTAINS
match_limit: 10
size: 60
placeholder: ''
third_party_settings: { }
hidden: { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
langcode: en
status: true
dependencies:
config:
- field.field.node.content_type_three.field_link_lists
- node.type.content_type_three
module:
- user
id: node.content_type_three.default
targetEntityType: node
bundle: content_type_three
mode: default
content:
field_link_lists:
type: entity_reference_entity_view
label: hidden
settings:
view_mode: default
link: true
third_party_settings: { }
weight: 101
region: content
links:
settings: { }
third_party_settings: { }
weight: 100
region: content
hidden:
search_api_excerpt: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
langcode: en
status: true
dependencies:
config:
- field.storage.node.field_link_lists
- node.type.content_type_three
- oe_link_lists.link_list_type.dynamic
id: node.content_type_three.field_link_lists
field_name: field_link_lists
entity_type: node
bundle: content_type_three
label: 'Link Lists'
description: ''
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
handler: 'default:link_list'
handler_settings:
target_bundles:
dynamic: dynamic
sort:
field: _none
direction: ASC
auto_create: false
auto_create_bundle: ''
field_type: entity_reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
langcode: en
status: true
dependencies:
module:
- node
- oe_link_lists
id: node.field_link_lists
field_name: field_link_lists
entity_type: node
type: entity_reference
settings:
target_type: link_list
module: core
locked: false
cardinality: -1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
langcode: en
status: true
dependencies: { }
name: 'Content type three'
type: content_type_three
description: ''
help: ''
new_revision: false
preview_mode: 1
display_submitted: false
Loading

0 comments on commit 8a7f2a2

Please sign in to comment.