I'm wondering if there's a way to update the "identifiers" (not sure of the terminology) of a key from the URL when setting an exposed form field to multi-select?
The key pattern in the URL is key%5B0%5D=238
.
Looking in the Request Stack $this->requestStack->getCurrentRequest()
, these options are returned as an array:
key
0 => 13
1 => 238
I'm currently working in a custom class class CustomBlock extends BlockBase implements ContainerFactoryPluginInterface
.
The intent is to create links for each filter option that remove an individual selection from the URL.
Edit: Below is the updated code I am working with. This all seems to work with testing, but I did expect the identifiers to update so am not 100%:
public function build() {
$q_parameters = $this->requestStack->getCurrentRequest()->query->all();
$pills = "";
foreach ($q_parameters as $key => $value) {
if ($key !== $someValue) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$newValue = $value;
unset($newValue[$k]);
$pillOptions = [
'query' => [
$key => $newValue, // This doesn't re-sequence the identifiers.
],
'attributes' => [
'class' => [
'class',
],
],
];
$pillUrl = Url::fromUserInput($this->requestStack->getCurrentRequest()->getRequestUri(), $pillOptions);
$pills .= Link::fromTextAndUrl($name, $pillUrl)->toString();
}
}
else {
$pillOptions = [
'query' => [
$key => "All",
],
'attributes' => [
'class' => [
'class',
],
],
];
$pillUrl = Url::fromUserInput($this->requestStack->getCurrentRequest()->getRequestUri(), $pillOptions);
$pills .= Link::fromTextAndUrl($name, $pillUrl)->toString();
}
}
}
return [
'#markup' => $pills,
];
}
If the array looks like:
key
0 => 13
1 => 238
The path query looks like:
key%5B0%5D=13&key%5B1%5D=238
The links generated do remove the unset array key, though the unique identifiers do not update, e.g. I would have thought they'd be reset to sequential %5B0
, %5B1
, etc.
The part in question is setting the array to the query attribute as:
$pillOptions = [
'query' => [
$key => $newValue,
],
];
Is it possible to re-sequence the key identifiers? Or is it even necessary?