@@ -12,6 +12,8 @@ use Drupal\Component\Utility\Html;
1212use Drupal \Core \Cache \Cache ;
1313use Drupal \Core \Form \FormStateInterface ;
1414use Drupal \Core \Template \Attribute ;
15+ use Drupal \views \Plugin \views \filter \FilterPluginBase ;
16+ use Drupal \views \ViewExecutable ;
1517use Drupal \views \Views ;
1618
1719/**
@@ -30,7 +32,7 @@ function civictheme_preprocess_views_view(array &$variables): void {
3032 * @SuppressWarnings(PHPMD.StaticAccess)
3133 */
3234function _civictheme_preprocess_views_view__view (array &$ variables ): void {
33- /** @var \Drupal\views\ ViewExecutable $view */
35+ /** @var ViewExecutable $view */
3436 $ view = &$ variables ['view ' ];
3537
3638 $ 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 {
6971 return ;
7072 }
7173
74+ /** @var ViewExecutable $view */
7275 $ view = &$ variables ['view ' ];
7376
7477 // Hide pager if there is only one result page.
@@ -255,7 +258,9 @@ function _civictheme_preprocess_views__exposed_form__group_filter(array &$variab
255258 * Preprocess views selected filters from the query params.
256259 */
257260function _civictheme_preprocess_views_view__selected_filters (array &$ variables ): void {
258- $ variables ['selected_filters ' ] = _civictheme_preprocess_views_view__selected_filters_list ();
261+ /** @var ViewExecutable $view */
262+ $ view = $ variables ['view ' ];
263+ $ variables ['selected_filters ' ] = _civictheme_preprocess_views_view__selected_filters_list ($ view );
259264
260265 if (!empty ($ variables ['selected_filters ' ])) {
261266 $ variables ['selected_filters_clear_link ' ] = [
@@ -271,6 +276,7 @@ function _civictheme_preprocess_views_view__selected_filters(array &$variables):
271276 * @SuppressWarnings(PHPMD.ElseExpression)
272277 */
273278function _civictheme_preprocess_views_view__search_page (array &$ variables ): void {
279+ /** @var ViewExecutable $view */
274280 $ view = $ variables ['view ' ];
275281 if ($ view ->id () == 'civictheme_search ' && $ view ->current_display == 'page_1 ' ) {
276282 $ variables ['vertical_spacing ' ] = 'top ' ;
@@ -280,7 +286,8 @@ function _civictheme_preprocess_views_view__search_page(array &$variables): void
280286/**
281287 * Preprocess the selected filters list.
282288 */
283- function _civictheme_preprocess_views_view__selected_filters_list (): array {
289+ function _civictheme_preprocess_views_view__selected_filters_list (ViewExecutable $ view ): array {
290+ $ filter_types = _civictheme_preprocess_views_view__selected_filters_get_filter_types ($ view );
284291 $ query_params = \Drupal::request ()->query ->all ();
285292 $ current_values = array_filter ($ query_params , function ($ value ) {
286293 return !empty ($ value );
@@ -304,7 +311,7 @@ function _civictheme_preprocess_views_view__selected_filters_list(): array {
304311 unset($ new_query_params [$ key ]);
305312 $ selected_filters [$ key ] = [
306313 'url ' => \Drupal::request ()->getUriForPath (\Drupal::request ()->getPathInfo ()) . '? ' . http_build_query ($ new_query_params ),
307- 'text ' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label ($ key , $ value ),
314+ 'text ' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label ($ key , $ value, $ filter_types ),
308315 ];
309316 continue ;
310317 }
@@ -318,7 +325,7 @@ function _civictheme_preprocess_views_view__selected_filters_list(): array {
318325 }
319326 $ selected_filters [$ key . '_ ' . $ item . '_ ' . $ value_key ] = [
320327 'url ' => \Drupal::request ()->getUriForPath (\Drupal::request ()->getPathInfo ()) . '? ' . http_build_query ($ temp_query_params ),
321- 'text ' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label ($ key , $ item ),
328+ 'text ' => _civictheme_preprocess_views_view__selected_filters_list__create_filter_label ($ key , $ item, $ filter_types ),
322329 ];
323330 }
324331 }
@@ -327,31 +334,60 @@ function _civictheme_preprocess_views_view__selected_filters_list(): array {
327334 return $ selected_filters ;
328335}
329336
337+ /**
338+ * Get the filter types from the view.
339+ *
340+ * @param ViewExecutable $view
341+ * The view executable object.
342+ *
343+ * @return array
344+ * Array of exposed filters with their properties:
345+ * - name: The human readable label
346+ * - type: The filter plugin ID
347+ * - machine_name: The filter ID
348+ */
349+ function _civictheme_preprocess_views_view__selected_filters_get_filter_types (ViewExecutable $ view ): array {
350+ $ exposed_filters = [];
351+ if (!empty ($ view )) {
352+ $ filters = $ view ->display_handler ->getHandlers ('filter ' );
353+ foreach ($ filters as $ filter_id => $ filter ) {
354+ if ($ filter instanceof FilterPluginBase && $ filter ->isExposed ()) {
355+ $ exposed_filters [$ filter ->options ['expose ' ]['identifier ' ]] = [
356+ 'name ' => $ filter ->options ['expose ' ]['label ' ],
357+ 'type ' => $ filter ->getPluginId (),
358+ 'machine_name ' => $ filter_id ,
359+ ];
360+ }
361+ }
362+ }
363+ return $ exposed_filters ;
364+ }
365+
330366/**
331367 * Create the label correctly for each of the selected filters.
332368 *
333369 * @param string $key
334370 * The filter key.
335371 * @param string|int $value
336372 * The filter value.
373+ * @param array $filter_types
374+ * Array of filter types containing metadata about each filter.
337375 *
338376 * @return string
339- * The formatted filter label.
377+ * The formatted filter label in the format "Key: Value" .
340378 */
341- function _civictheme_preprocess_views_view__selected_filters_list__create_filter_label (string $ key , string |int $ value ): string {
342- switch ($ key ) {
343- case 'type ' :
344- // Get the type label from the node type.
379+ function _civictheme_preprocess_views_view__selected_filters_list__create_filter_label (string $ key , string |int $ value , array $ filter_types ): string {
380+ switch ($ filter_types [$ key ]['type ' ]) {
381+ case 'bundle ' :
345382 $ node_type = \Drupal::entityTypeManager ()->getStorage ('node_type ' )->load ($ value );
346383 $ value_label = $ node_type ? $ node_type ->label () : $ value ;
347384 break ;
348- case 'topic ' :
349- // Get the topic label from the topic term.
350- $ topic_term = \Drupal::entityTypeManager ()->getStorage ('taxonomy_term ' )->load ($ value );
351- $ value_label = $ topic_term ? $ topic_term ->label () : $ value ;
385+ case 'taxonomy_index_tid ' :
386+ $ taxonomy_term = \Drupal::entityTypeManager ()->getStorage ('taxonomy_term ' )->load ($ value );
387+ $ value_label = $ taxonomy_term ? $ taxonomy_term ->label () : $ value ;
352388 break ;
353389 default :
354390 $ value_label = $ value ;
355391 }
356- return ucfirst ( str_replace ( ' _ ' , ' ' , $ key)) . ': ' . $ value_label ;
392+ return $ filter_types [ $ key][ ' name ' ] . ': ' . $ value_label ;
357393}
0 commit comments