function template_preprocess_bef_links in Better Exposed Filters 8.3
Same name and namespace in other branches
- 8.5 includes/better_exposed_filters.theme.inc \template_preprocess_bef_links()
- 8.4 includes/better_exposed_filters.theme.inc \template_preprocess_bef_links()
File
- ./
better_exposed_filters.module, line 140 - Allows the use of checkboxes, radio buttons or hidden fields for exposed Views filters.
Code
function template_preprocess_bef_links(&$variables) {
// Collect some variables before we start tweaking the element.
$element =& $variables['element'];
$options = $element['#options'];
$name = $element['#name'];
// Get the query string arguments from the current request.
$existingQuery = \Drupal::service('request_stack')
->getCurrentRequest()->query
->all();
// Remove page parameter from query.
unset($existingQuery['page']);
// Pass currently selected values for this filter to the template as an array.
$variables['selected'] = $element['#value'];
if (!is_array($variables['selected'])) {
$variables['selected'] = [
$variables['selected'],
];
}
$variables['links'] = [];
foreach ($options as $optionValue => $optionLabel) {
// Build a new Url object for each link since the query string changes with
// each option.
/** @var Drupal\Core\Url $url */
$url = clone $element['#bef_path'];
// Allow visitors to toggle a filter setting on and off. This is not as
// simple as setOptions('foo', '') as that still leaves an entry which is
// rendered rather than removing the entry from the query string altogether.
// Calling $url->setOption() still leaves a value behind. Instead we work
// with the entire options array and remove items from it as needed.
$urlOptions = $url
->getOptions();
if ($element['#multiple']) {
$selectedValues = $element['#value'];
$newQuery = isset($existingQuery[$name]) ? $existingQuery[$name] : [];
if (in_array($optionValue, $selectedValues)) {
// Allow users to toggle an option using the same link.
$newQuery = array_filter($newQuery, function ($value) use ($selectedValues) {
return !in_array($value, $selectedValues);
});
}
else {
$newQuery[] = $optionValue;
}
if (empty($newQuery)) {
unset($urlOptions['query'][$name]);
}
else {
$urlOptions['query'][$name] = $newQuery;
}
}
else {
if ($optionValue == $element['#value']) {
// Allow toggle link functionality -- click the same link to turn an
// option on or off.
$newQuery = $existingQuery;
unset($newQuery[$name]);
if (empty($newQuery)) {
// Remove the query string completely.
unset($urlOptions['query']);
}
else {
$urlOptions['query'] = $newQuery;
}
}
else {
$urlOptions['query'] = $existingQuery;
$urlOptions['query'][$name] = $optionValue;
}
}
// Add our updated options to the Url object.
$url
->setOptions($urlOptions);
// Provide the Twig template with an array of links.
$variables['links'][$optionValue] = [
'#type' => 'link',
'#title' => $optionLabel,
'#theme_wrappers' => [
'container',
],
'#url' => $url,
];
}
// Handle nested links. But first add the links as children to the element
// for consistent processing between checkboxes/radio buttons and links.
$variables['element'] = array_replace($variables['element'], $variables['links']);
$variables['children'] = Element::children($variables['element']);
_bef_preprocess_nested_elements($variables);
}