From c48a3b9d8995eee5898838728f636bc0e69a8849 Mon Sep 17 00:00:00 2001 From: Feb Dao Date: Fri, 14 Mar 2025 13:26:17 +1100 Subject: [PATCH 01/12] [CIVIC-2105] Selected filters. --- .../contrib/civictheme/includes/views.inc | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/web/themes/contrib/civictheme/includes/views.inc b/web/themes/contrib/civictheme/includes/views.inc index 244fc2f2d6..b2ffb6a5c5 100644 --- a/web/themes/contrib/civictheme/includes/views.inc +++ b/web/themes/contrib/civictheme/includes/views.inc @@ -21,6 +21,7 @@ function civictheme_preprocess_views_view(array &$variables): void { _civictheme_preprocess_views_view__view($variables); _civictheme_preprocess_views_view__pager($variables); _civictheme_preprocess_views_view__search_page($variables); + _civictheme_preprocess_views__selected_filters($variables); } /** @@ -250,6 +251,20 @@ function _civictheme_preprocess_views__exposed_form__group_filter(array &$variab $variables['group_filter'] = TRUE; } +/** + * Preprocess views selected filters from the query params. + */ +function _civictheme_preprocess_views__selected_filters(array &$variables): void { + $variables['selected_filters'] = _civictheme_automated_list__selected_filters(); + + if (!empty($variables['selected_filters'])) { + $variables['selected_filters_clear_link'] = [ + 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query([]), + 'text' => t('Clear all'), + 'attributes' => 'aria-label="' . t('Clear all filters') . '"', + ]; + } +} /** * Preprocess for a search page. * @@ -261,3 +276,75 @@ function _civictheme_preprocess_views_view__search_page(array &$variables): void $variables['vertical_spacing'] = 'top'; } } + +/** + * Get an array of the active filters and format them for the selected filters. + */ +function _civictheme_automated_list__selected_filters(): array { + $query_params = \Drupal::request()->query->all(); + $current_values = array_filter($query_params, function ($value) { + return !empty($value); + }); + + // Prevent other query params from being used in the selected filters. + $permitted_keys = [ + 'type', + 'topic', + 'title', + 'items_per_page', + ]; // Change this to get from $view there is an exposed input field or something that gives allowed exposed filters, pass $view in as argument to this function + // this function should be called after the hook for changing view + $selected_filters = []; + foreach ($current_values as $key => $value) { + if (!in_array($key, $permitted_keys)) { + continue; + } + $new_query_params = $query_params; + if (is_string($value)) { + unset($new_query_params[$key]); + $selected_filters[$key] = [ + 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query($new_query_params), + 'text' => _civictheme_automated_list__create_filter_label($key, $value), + ]; + continue; + } + if (is_array($value)) { + foreach ($value as $value_key => $item) { + if (!empty($item)) { + $temp_query_params = $query_params; + unset($temp_query_params[$key][$value_key]); + if (empty($temp_query_params[$key])) { + unset($temp_query_params[$key]); + } + $selected_filters[$key . '_' . $item . '_' . $value_key] = [ + 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query($temp_query_params), + 'text' => _civictheme_automated_list__create_filter_label($key, $item, $value_key), + ]; + } + } + } + } + return $selected_filters; +} + +/** + * Create the label correctly for each of the selected filters. + */ +function _civictheme_automated_list__create_filter_label(string $key, $value, $value_key = NULL): string { + // Get the value label. + switch ($key) { + case 'type': + // Get the type label from the node type. + $node_type = \Drupal::entityTypeManager()->getStorage('node_type')->load($value); + $value_label = $node_type ? $node_type->label() : $value; + break; + case 'topic': + // Get the topic label from the topic term. + $topic_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($value); + $value_label = $topic_term ? $topic_term->label() : $value; + break; + default: + $value_label = $value; + } + return ucfirst(str_replace('_', ' ', $key)) . ': ' . $value_label; +} From 5397039fed6d8797b3285dddaa5a4d0cef485a2b Mon Sep 17 00:00:00 2001 From: Feb Dao Date: Fri, 14 Mar 2025 14:25:30 +1100 Subject: [PATCH 02/12] [CIVIC-2105] Fixed lint. --- web/themes/contrib/civictheme/includes/views.inc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/web/themes/contrib/civictheme/includes/views.inc b/web/themes/contrib/civictheme/includes/views.inc index b2ffb6a5c5..62e23b1542 100644 --- a/web/themes/contrib/civictheme/includes/views.inc +++ b/web/themes/contrib/civictheme/includes/views.inc @@ -318,7 +318,7 @@ function _civictheme_automated_list__selected_filters(): array { } $selected_filters[$key . '_' . $item . '_' . $value_key] = [ 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query($temp_query_params), - 'text' => _civictheme_automated_list__create_filter_label($key, $item, $value_key), + 'text' => _civictheme_automated_list__create_filter_label($key, $item), ]; } } @@ -329,8 +329,16 @@ function _civictheme_automated_list__selected_filters(): array { /** * Create the label correctly for each of the selected filters. + * + * @param string $key + * The filter key. + * @param string|int $value + * The filter value. + * + * @return string + * The formatted filter label. */ -function _civictheme_automated_list__create_filter_label(string $key, $value, $value_key = NULL): string { +function _civictheme_automated_list__create_filter_label(string $key, string|int $value): string { // Get the value label. switch ($key) { case 'type': From 064b40cdc6741a34add237d58592b18f71630b20 Mon Sep 17 00:00:00 2001 From: Feb Dao Date: Fri, 14 Mar 2025 14:31:12 +1100 Subject: [PATCH 03/12] [CIVIC-2105] Update uiKit for selecter filters testing. --- web/themes/contrib/civictheme/package-lock.json | 5 ++--- web/themes/contrib/civictheme/package.json | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/web/themes/contrib/civictheme/package-lock.json b/web/themes/contrib/civictheme/package-lock.json index edd4b7ec64..34f81a085a 100644 --- a/web/themes/contrib/civictheme/package-lock.json +++ b/web/themes/contrib/civictheme/package-lock.json @@ -21,7 +21,7 @@ "win32" ], "dependencies": { - "@civictheme/uikit": "github:civictheme/uikit#v1.10.0", + "@civictheme/uikit": "github:civictheme/uikit#feature/497-Selected-filters", "@popperjs/core": "^2.11.8" }, "devDependencies": { @@ -100,8 +100,7 @@ }, "node_modules/@civictheme/uikit": { "version": "1.9.0", - "resolved": "git+ssh://git@github.com/civictheme/uikit.git#16aaff9a17a9b530168beeffb10a175450e4e20b", - "integrity": "sha512-HczPLfCf0aJCoLeQ38kzdyW4vl8O074w9SnRpRTW8gToIyVSuU9rLIScBO4Zk50LrVOcwLQpLmBXxd8RcV7X+g==", + "resolved": "git+ssh://git@github.com/civictheme/uikit.git#53df710c3a49df7f8f0f48dfc99dd81bf89575b9", "license": "GPL-2.0-or-later", "dependencies": { "@popperjs/core": "^2.11.8" diff --git a/web/themes/contrib/civictheme/package.json b/web/themes/contrib/civictheme/package.json index 389dc898a1..8c6f55fd4b 100644 --- a/web/themes/contrib/civictheme/package.json +++ b/web/themes/contrib/civictheme/package.json @@ -38,7 +38,7 @@ "uikit-install": "rm -Rf components > /dev/null 2>&1 && cp -R node_modules/@civictheme/uikit/components components" }, "dependencies": { - "@civictheme/uikit": "github:civictheme/uikit#v1.10.0", + "@civictheme/uikit": "github:civictheme/uikit#feature/497-Selected-filters", "@popperjs/core": "^2.11.8" }, "devDependencies": { From 33f05090612602c22ee5d9b661c404e4f139258a Mon Sep 17 00:00:00 2001 From: Feb Dao Date: Fri, 14 Mar 2025 15:18:30 +1100 Subject: [PATCH 04/12] [CIVIC-2105] Rerun test. From 76ee1930bfbfac478efa42b8aa7f584c065a2e7e Mon Sep 17 00:00:00 2001 From: Feb Dao Date: Mon, 24 Mar 2025 14:18:01 +1100 Subject: [PATCH 05/12] [#3510578] Fixed CR. --- .../contrib/civictheme/includes/views.inc | 23 +++++++++---------- .../contrib/civictheme/package-lock.json | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/web/themes/contrib/civictheme/includes/views.inc b/web/themes/contrib/civictheme/includes/views.inc index 62e23b1542..af0b70275b 100644 --- a/web/themes/contrib/civictheme/includes/views.inc +++ b/web/themes/contrib/civictheme/includes/views.inc @@ -21,7 +21,7 @@ function civictheme_preprocess_views_view(array &$variables): void { _civictheme_preprocess_views_view__view($variables); _civictheme_preprocess_views_view__pager($variables); _civictheme_preprocess_views_view__search_page($variables); - _civictheme_preprocess_views__selected_filters($variables); + _civictheme_preprocess_views_view__selected_filters($variables); } /** @@ -254,8 +254,8 @@ function _civictheme_preprocess_views__exposed_form__group_filter(array &$variab /** * Preprocess views selected filters from the query params. */ -function _civictheme_preprocess_views__selected_filters(array &$variables): void { - $variables['selected_filters'] = _civictheme_automated_list__selected_filters(); +function _civictheme_preprocess_views_view__selected_filters(array &$variables): void { + $variables['selected_filters'] = _civictheme_preprocess_views_view__selected_filters_list(); if (!empty($variables['selected_filters'])) { $variables['selected_filters_clear_link'] = [ @@ -278,9 +278,9 @@ function _civictheme_preprocess_views_view__search_page(array &$variables): void } /** - * Get an array of the active filters and format them for the selected filters. + * Preprocess the selected filters list. */ -function _civictheme_automated_list__selected_filters(): array { +function _civictheme_preprocess_views_view__selected_filters_list(): array { $query_params = \Drupal::request()->query->all(); $current_values = array_filter($query_params, function ($value) { return !empty($value); @@ -292,9 +292,9 @@ function _civictheme_automated_list__selected_filters(): array { 'topic', 'title', 'items_per_page', - ]; // Change this to get from $view there is an exposed input field or something that gives allowed exposed filters, pass $view in as argument to this function - // this function should be called after the hook for changing view - $selected_filters = []; + ]; + $selected_filters = []; + foreach ($current_values as $key => $value) { if (!in_array($key, $permitted_keys)) { continue; @@ -304,7 +304,7 @@ function _civictheme_automated_list__selected_filters(): array { unset($new_query_params[$key]); $selected_filters[$key] = [ 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query($new_query_params), - 'text' => _civictheme_automated_list__create_filter_label($key, $value), + 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $value), ]; continue; } @@ -318,7 +318,7 @@ function _civictheme_automated_list__selected_filters(): array { } $selected_filters[$key . '_' . $item . '_' . $value_key] = [ 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query($temp_query_params), - 'text' => _civictheme_automated_list__create_filter_label($key, $item), + 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $item), ]; } } @@ -338,8 +338,7 @@ function _civictheme_automated_list__selected_filters(): array { * @return string * The formatted filter label. */ -function _civictheme_automated_list__create_filter_label(string $key, string|int $value): string { - // Get the value label. +function _civictheme_preprocess_views_view__selected_filters_list__create_filter_label(string $key, string|int $value): string { switch ($key) { case 'type': // Get the type label from the node type. diff --git a/web/themes/contrib/civictheme/package-lock.json b/web/themes/contrib/civictheme/package-lock.json index 34f81a085a..8295ceff74 100644 --- a/web/themes/contrib/civictheme/package-lock.json +++ b/web/themes/contrib/civictheme/package-lock.json @@ -100,7 +100,7 @@ }, "node_modules/@civictheme/uikit": { "version": "1.9.0", - "resolved": "git+ssh://git@github.com/civictheme/uikit.git#53df710c3a49df7f8f0f48dfc99dd81bf89575b9", + "resolved": "git+ssh://git@github.com/civictheme/uikit.git#0a0a38e4dca1f514a3d82b39bd80f53267a0fbef", "license": "GPL-2.0-or-later", "dependencies": { "@popperjs/core": "^2.11.8" From 44b35d7d41244a794f63ca6e4745d6ddd62cdd1f Mon Sep 17 00:00:00 2001 From: Alan Cole Date: Wed, 14 May 2025 13:53:16 +1000 Subject: [PATCH 06/12] [#3510578] Fixed create_filter_label to use generic types to get labels. --- .../contrib/civictheme/includes/views.inc | 66 ++++++++++++++----- .../contrib/civictheme/package-lock.json | 4 +- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/web/themes/contrib/civictheme/includes/views.inc b/web/themes/contrib/civictheme/includes/views.inc index af0b70275b..7f12d14dc1 100644 --- a/web/themes/contrib/civictheme/includes/views.inc +++ b/web/themes/contrib/civictheme/includes/views.inc @@ -12,6 +12,8 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Cache\Cache; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Template\Attribute; +use Drupal\views\Plugin\views\filter\FilterPluginBase; +use Drupal\views\ViewExecutable; use Drupal\views\Views; /** @@ -30,7 +32,7 @@ function civictheme_preprocess_views_view(array &$variables): void { * @SuppressWarnings(PHPMD.StaticAccess) */ function _civictheme_preprocess_views_view__view(array &$variables): void { - /** @var \Drupal\views\ViewExecutable $view */ + /** @var ViewExecutable $view */ $view = &$variables['view']; $variables['attributes']['class'] = empty($variables['dom_id']) ? Html::getUniqueId('js-view-dom-id') : 'js-view-dom-id-' . $variables['dom_id']; @@ -69,6 +71,7 @@ function _civictheme_preprocess_views_view__pager(array &$variables): void { return; } + /** @var ViewExecutable $view */ $view = &$variables['view']; // Hide pager if there is only one result page. @@ -255,7 +258,9 @@ function _civictheme_preprocess_views__exposed_form__group_filter(array &$variab * Preprocess views selected filters from the query params. */ function _civictheme_preprocess_views_view__selected_filters(array &$variables): void { - $variables['selected_filters'] = _civictheme_preprocess_views_view__selected_filters_list(); + /** @var ViewExecutable $view */ + $view = $variables['view']; + $variables['selected_filters'] = _civictheme_preprocess_views_view__selected_filters_list($view); if (!empty($variables['selected_filters'])) { $variables['selected_filters_clear_link'] = [ @@ -271,6 +276,7 @@ function _civictheme_preprocess_views_view__selected_filters(array &$variables): * @SuppressWarnings(PHPMD.ElseExpression) */ function _civictheme_preprocess_views_view__search_page(array &$variables): void { + /** @var ViewExecutable $view */ $view = $variables['view']; if ($view->id() == 'civictheme_search' && $view->current_display == 'page_1') { $variables['vertical_spacing'] = 'top'; @@ -280,7 +286,8 @@ function _civictheme_preprocess_views_view__search_page(array &$variables): void /** * Preprocess the selected filters list. */ -function _civictheme_preprocess_views_view__selected_filters_list(): array { +function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable $view): array { + $filter_types = _civictheme_preprocess_views_view__selected_filters_get_filter_types($view); $query_params = \Drupal::request()->query->all(); $current_values = array_filter($query_params, function ($value) { return !empty($value); @@ -304,7 +311,7 @@ function _civictheme_preprocess_views_view__selected_filters_list(): array { unset($new_query_params[$key]); $selected_filters[$key] = [ 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query($new_query_params), - 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $value), + 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $value, $filter_types), ]; continue; } @@ -318,7 +325,7 @@ function _civictheme_preprocess_views_view__selected_filters_list(): array { } $selected_filters[$key . '_' . $item . '_' . $value_key] = [ 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query($temp_query_params), - 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $item), + 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $item, $filter_types), ]; } } @@ -327,6 +334,35 @@ function _civictheme_preprocess_views_view__selected_filters_list(): array { return $selected_filters; } +/** + * Get the filter types from the view. + * + * @param ViewExecutable $view + * The view executable object. + * + * @return array + * Array of exposed filters with their properties: + * - name: The human readable label + * - type: The filter plugin ID + * - machine_name: The filter ID + */ +function _civictheme_preprocess_views_view__selected_filters_get_filter_types(ViewExecutable $view): array { + $exposed_filters = []; + if (!empty($view)) { + $filters = $view->display_handler->getHandlers('filter'); + foreach ($filters as $filter_id => $filter) { + if ($filter instanceof FilterPluginBase && $filter->isExposed()) { + $exposed_filters[$filter->options['expose']['identifier']] = [ + 'name' => $filter->options['expose']['label'], + 'type' => $filter->getPluginId(), + 'machine_name' => $filter_id, + ]; + } + } + } + return $exposed_filters; +} + /** * Create the label correctly for each of the selected filters. * @@ -334,24 +370,24 @@ function _civictheme_preprocess_views_view__selected_filters_list(): array { * The filter key. * @param string|int $value * The filter value. + * @param array $filter_types + * Array of filter types containing metadata about each filter. * * @return string - * The formatted filter label. + * The formatted filter label in the format "Key: Value". */ -function _civictheme_preprocess_views_view__selected_filters_list__create_filter_label(string $key, string|int $value): string { - switch ($key) { - case 'type': - // Get the type label from the node type. +function _civictheme_preprocess_views_view__selected_filters_list__create_filter_label(string $key, string|int $value, array $filter_types): string { + switch ($filter_types[$key]['type']) { + case 'bundle': $node_type = \Drupal::entityTypeManager()->getStorage('node_type')->load($value); $value_label = $node_type ? $node_type->label() : $value; break; - case 'topic': - // Get the topic label from the topic term. - $topic_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($value); - $value_label = $topic_term ? $topic_term->label() : $value; + case 'taxonomy_index_tid': + $taxonomy_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($value); + $value_label = $taxonomy_term ? $taxonomy_term->label() : $value; break; default: $value_label = $value; } - return ucfirst(str_replace('_', ' ', $key)) . ': ' . $value_label; + return $filter_types[$key]['name'] . ': ' . $value_label; } diff --git a/web/themes/contrib/civictheme/package-lock.json b/web/themes/contrib/civictheme/package-lock.json index 8295ceff74..488968e4a0 100644 --- a/web/themes/contrib/civictheme/package-lock.json +++ b/web/themes/contrib/civictheme/package-lock.json @@ -99,8 +99,8 @@ "license": "(Apache-2.0 AND BSD-3-Clause)" }, "node_modules/@civictheme/uikit": { - "version": "1.9.0", - "resolved": "git+ssh://git@github.com/civictheme/uikit.git#0a0a38e4dca1f514a3d82b39bd80f53267a0fbef", + "version": "1.10.0", + "resolved": "git+ssh://git@github.com/civictheme/uikit.git#14834bef8520070cb238092840ee081a398912ad", "license": "GPL-2.0-or-later", "dependencies": { "@popperjs/core": "^2.11.8" From 66dc10ca3b5bc4cfca110f9adaa53f4f67ea16b4 Mon Sep 17 00:00:00 2001 From: Alan Cole Date: Wed, 14 May 2025 14:05:06 +1000 Subject: [PATCH 07/12] [#3510578] Fixed linting issues. --- web/themes/contrib/civictheme/includes/views.inc | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/web/themes/contrib/civictheme/includes/views.inc b/web/themes/contrib/civictheme/includes/views.inc index 7f12d14dc1..8979fc3f05 100644 --- a/web/themes/contrib/civictheme/includes/views.inc +++ b/web/themes/contrib/civictheme/includes/views.inc @@ -32,7 +32,7 @@ function civictheme_preprocess_views_view(array &$variables): void { * @SuppressWarnings(PHPMD.StaticAccess) */ function _civictheme_preprocess_views_view__view(array &$variables): void { - /** @var ViewExecutable $view */ + /** @var \Drupal\views\ViewExecutable $view */ $view = &$variables['view']; $variables['attributes']['class'] = empty($variables['dom_id']) ? Html::getUniqueId('js-view-dom-id') : 'js-view-dom-id-' . $variables['dom_id']; @@ -71,7 +71,7 @@ function _civictheme_preprocess_views_view__pager(array &$variables): void { return; } - /** @var ViewExecutable $view */ + /** @var \Drupal\views\ViewExecutable $view */ $view = &$variables['view']; // Hide pager if there is only one result page. @@ -258,7 +258,7 @@ function _civictheme_preprocess_views__exposed_form__group_filter(array &$variab * Preprocess views selected filters from the query params. */ function _civictheme_preprocess_views_view__selected_filters(array &$variables): void { - /** @var ViewExecutable $view */ + /** @var \Drupal\views\ViewExecutable $view */ $view = $variables['view']; $variables['selected_filters'] = _civictheme_preprocess_views_view__selected_filters_list($view); @@ -270,13 +270,14 @@ function _civictheme_preprocess_views_view__selected_filters(array &$variables): ]; } } + /** * Preprocess for a search page. * * @SuppressWarnings(PHPMD.ElseExpression) */ function _civictheme_preprocess_views_view__search_page(array &$variables): void { - /** @var ViewExecutable $view */ + /** @var \Drupal\views\ViewExecutable $view */ $view = $variables['view']; if ($view->id() == 'civictheme_search' && $view->current_display == 'page_1') { $variables['vertical_spacing'] = 'top'; @@ -337,7 +338,7 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable /** * Get the filter types from the view. * - * @param ViewExecutable $view + * @param \Drupal\views\ViewExecutable $view * The view executable object. * * @return array @@ -382,10 +383,12 @@ function _civictheme_preprocess_views_view__selected_filters_list__create_filter $node_type = \Drupal::entityTypeManager()->getStorage('node_type')->load($value); $value_label = $node_type ? $node_type->label() : $value; break; + case 'taxonomy_index_tid': $taxonomy_term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($value); $value_label = $taxonomy_term ? $taxonomy_term->label() : $value; break; + default: $value_label = $value; } From 20a86b9a04bdfc5a87dbb755d5b45dc654e3f2b3 Mon Sep 17 00:00:00 2001 From: Alan Cole Date: Wed, 14 May 2025 14:44:17 +1000 Subject: [PATCH 08/12] [#3510578] Avoid issues in ajax by disabling selected filters on AJAX. --- web/themes/contrib/civictheme/includes/views.inc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/web/themes/contrib/civictheme/includes/views.inc b/web/themes/contrib/civictheme/includes/views.inc index 8979fc3f05..e724f3ab85 100644 --- a/web/themes/contrib/civictheme/includes/views.inc +++ b/web/themes/contrib/civictheme/includes/views.inc @@ -260,6 +260,12 @@ function _civictheme_preprocess_views__exposed_form__group_filter(array &$variab function _civictheme_preprocess_views_view__selected_filters(array &$variables): void { /** @var \Drupal\views\ViewExecutable $view */ $view = $variables['view']; + + // Selected filters does not currently support ajax. + if ($view->ajaxEnabled()) { + return; + } + $variables['selected_filters'] = _civictheme_preprocess_views_view__selected_filters_list($view); if (!empty($variables['selected_filters'])) { @@ -294,13 +300,8 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable return !empty($value); }); - // Prevent other query params from being used in the selected filters. - $permitted_keys = [ - 'type', - 'topic', - 'title', - 'items_per_page', - ]; + // Only process keys for available filters. + $permitted_keys = array_keys($filter_types); $selected_filters = []; foreach ($current_values as $key => $value) { From ea2f5d7b37770bf1cfdd12226db9651217bd33f9 Mon Sep 17 00:00:00 2001 From: Alan Cole Date: Wed, 14 May 2025 15:03:40 +1000 Subject: [PATCH 09/12] [#3510578] Improve link paths. This partially resolves ajax issues. --- web/themes/contrib/civictheme/includes/views.inc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/web/themes/contrib/civictheme/includes/views.inc b/web/themes/contrib/civictheme/includes/views.inc index e724f3ab85..27dd21008e 100644 --- a/web/themes/contrib/civictheme/includes/views.inc +++ b/web/themes/contrib/civictheme/includes/views.inc @@ -270,7 +270,7 @@ function _civictheme_preprocess_views_view__selected_filters(array &$variables): if (!empty($variables['selected_filters'])) { $variables['selected_filters_clear_link'] = [ - 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query([]), + 'url' => \Drupal::service('path.current')->getPath(), 'text' => t('Clear all'), 'attributes' => 'aria-label="' . t('Clear all filters') . '"', ]; @@ -303,6 +303,7 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable // Only process keys for available filters. $permitted_keys = array_keys($filter_types); $selected_filters = []; + $base_url = \Drupal::service('path.current')->getPath(); foreach ($current_values as $key => $value) { if (!in_array($key, $permitted_keys)) { @@ -312,7 +313,7 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable if (is_string($value)) { unset($new_query_params[$key]); $selected_filters[$key] = [ - 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query($new_query_params), + 'url' => $base_url . '?' . http_build_query($new_query_params), 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $value, $filter_types), ]; continue; @@ -326,7 +327,7 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable unset($temp_query_params[$key]); } $selected_filters[$key . '_' . $item . '_' . $value_key] = [ - 'url' => \Drupal::request()->getUriForPath(\Drupal::request()->getPathInfo()) . '?' . http_build_query($temp_query_params), + 'url' => $base_url . '?' . http_build_query($temp_query_params), 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $item, $filter_types), ]; } From f181c41406d507f8de84974ef5a820111761a7e6 Mon Sep 17 00:00:00 2001 From: Alan Cole Date: Wed, 14 May 2025 15:12:12 +1000 Subject: [PATCH 10/12] [#3510578] Removed unnecessary check. --- .../contrib/civictheme/includes/views.inc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/web/themes/contrib/civictheme/includes/views.inc b/web/themes/contrib/civictheme/includes/views.inc index 27dd21008e..18fe7668a5 100644 --- a/web/themes/contrib/civictheme/includes/views.inc +++ b/web/themes/contrib/civictheme/includes/views.inc @@ -351,16 +351,14 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable */ function _civictheme_preprocess_views_view__selected_filters_get_filter_types(ViewExecutable $view): array { $exposed_filters = []; - if (!empty($view)) { - $filters = $view->display_handler->getHandlers('filter'); - foreach ($filters as $filter_id => $filter) { - if ($filter instanceof FilterPluginBase && $filter->isExposed()) { - $exposed_filters[$filter->options['expose']['identifier']] = [ - 'name' => $filter->options['expose']['label'], - 'type' => $filter->getPluginId(), - 'machine_name' => $filter_id, - ]; - } + $filters = $view->display_handler->getHandlers('filter'); + foreach ($filters as $filter_id => $filter) { + if ($filter instanceof FilterPluginBase && $filter->isExposed()) { + $exposed_filters[$filter->options['expose']['identifier']] = [ + 'name' => $filter->options['expose']['label'], + 'type' => $filter->getPluginId(), + 'machine_name' => $filter_id, + ]; } } return $exposed_filters; From e82077e63f13e2d17f990dba1ff5363914750c5e Mon Sep 17 00:00:00 2001 From: Alan Cole Date: Wed, 14 May 2025 20:25:20 +1000 Subject: [PATCH 11/12] [#3510578] Updated base branch. --- web/themes/contrib/civictheme/package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/themes/contrib/civictheme/package-lock.json b/web/themes/contrib/civictheme/package-lock.json index 488968e4a0..d81dda5f25 100644 --- a/web/themes/contrib/civictheme/package-lock.json +++ b/web/themes/contrib/civictheme/package-lock.json @@ -100,7 +100,7 @@ }, "node_modules/@civictheme/uikit": { "version": "1.10.0", - "resolved": "git+ssh://git@github.com/civictheme/uikit.git#14834bef8520070cb238092840ee081a398912ad", + "resolved": "git+ssh://git@github.com/civictheme/uikit.git#ce9ff5b7cc02140bdc5fd4d2dd9e3e062439365b", "license": "GPL-2.0-or-later", "dependencies": { "@popperjs/core": "^2.11.8" From f106a3b7d095db7aee5f16d5b9473b76faea4d8b Mon Sep 17 00:00:00 2001 From: Alan Cole Date: Thu, 15 May 2025 09:31:48 +1000 Subject: [PATCH 12/12] [#3510578] Fixes from CR. --- .../contrib/civictheme/includes/views.inc | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/web/themes/contrib/civictheme/includes/views.inc b/web/themes/contrib/civictheme/includes/views.inc index 18fe7668a5..f12405c04a 100644 --- a/web/themes/contrib/civictheme/includes/views.inc +++ b/web/themes/contrib/civictheme/includes/views.inc @@ -12,6 +12,7 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Cache\Cache; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Template\Attribute; +use Drupal\Core\Url; use Drupal\views\Plugin\views\filter\FilterPluginBase; use Drupal\views\ViewExecutable; use Drupal\views\Views; @@ -270,7 +271,7 @@ function _civictheme_preprocess_views_view__selected_filters(array &$variables): if (!empty($variables['selected_filters'])) { $variables['selected_filters_clear_link'] = [ - 'url' => \Drupal::service('path.current')->getPath(), + 'url' => Url::fromRoute('')->toString(), 'text' => t('Clear all'), 'attributes' => 'aria-label="' . t('Clear all filters') . '"', ]; @@ -303,7 +304,7 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable // Only process keys for available filters. $permitted_keys = array_keys($filter_types); $selected_filters = []; - $base_url = \Drupal::service('path.current')->getPath(); + $base_url = Url::fromRoute('')->toString(); foreach ($current_values as $key => $value) { if (!in_array($key, $permitted_keys)) { @@ -312,10 +313,8 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable $new_query_params = $query_params; if (is_string($value)) { unset($new_query_params[$key]); - $selected_filters[$key] = [ - 'url' => $base_url . '?' . http_build_query($new_query_params), - 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $value, $filter_types), - ]; + $filter_url = _civictheme_preprocess_views_view__selected_filters_list_create_filter_url($base_url, $new_query_params, $key, $value, $filter_types); + $selected_filters[$key] = $filter_url; continue; } if (is_array($value)) { @@ -326,10 +325,8 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable if (empty($temp_query_params[$key])) { unset($temp_query_params[$key]); } - $selected_filters[$key . '_' . $item . '_' . $value_key] = [ - 'url' => $base_url . '?' . http_build_query($temp_query_params), - 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $item, $filter_types), - ]; + $filter_url = _civictheme_preprocess_views_view__selected_filters_list_create_filter_url($base_url, $temp_query_params, $key, $item, $filter_types); + $selected_filters[$key . '_' . $item . '_' . $value_key] = $filter_url; } } } @@ -337,6 +334,17 @@ function _civictheme_preprocess_views_view__selected_filters_list(ViewExecutable return $selected_filters; } +/** + * Create the URL for the selected filter. + */ +function _civictheme_preprocess_views_view__selected_filters_list_create_filter_url(string $base_url, array $query_params, string $key, string|int $value, array $filter_types): array { + $query = http_build_query($query_params); + return [ + 'url' => !empty($query) ? ($base_url . '?' . $query) : $base_url, + 'text' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label($key, $value, $filter_types), + ]; +} + /** * Get the filter types from the view. *