- Issue created by @fionamorrison23
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
selected_filters
to $variables
in civictheme_preprocess_paragraph__civictheme_automated_list()
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
Active
1.9
Code