Problem/Motivation

We're adding selected filter chips to the group filter. These have been in the Figma file but not previously developed.
This is linked to the following GitHub issue:https://github.com/civictheme/uikit/issues/497

Drupal Implementation

  1. In automated_list.inc
    1. Add selected_filters to $variables in civictheme_preprocess_paragraph__civictheme_automated_list()
      1. Fields that display in automated lists, like 'title', 'type' and 'topic' should be supported.
    2. Add clear URL

Starting point for generating the filters - please update these functions as required:

// Add to civictheme_preprocess_paragraph__civictheme_automated_list()
$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') . '"',
  ];
}
/**
 * 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',
  ]; // 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, $value, $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 {
  return ucfirst(str_replace('_', ' ', $key)) . ': ' . $value;
}

3. Add tests to demonstrate this functionality in behat

Comments

fionamorrison23 created an issue. See original summary.

fionamorrison23’s picture

Version: 1.9.0 » 1.x-dev
fionamorrison23’s picture

fionamorrison23’s picture

Assigned: Unassigned » richardgaunt
Status: Active » Needs review
fionamorrison23’s picture

richardgaunt’s picture

Assigned: richardgaunt » danielgry

  • febdao authored da4e9ee7 on 1.x
    Issue #3510578: Group filter: integrate the selected filters feature...
richardgaunt’s picture

  • d430774b committed on 1.x
    #3510578 Fixed selected filters integration bug. (#1514)
    
richardgaunt’s picture

Status: Needs review » Reviewed & tested by the community
richardgaunt’s picture

Version: 1.x-dev » 1.13.0
Assigned: danielgry » Unassigned
Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.