Score:0

How to re-write exposed form multi-select query key identifiers?

ve flag

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?

Jaypan avatar
de flag
You've told us how you are trying to do something, maybe tell us what you are trying to do (aka your goal, or the reason for doing this)? It could help add context.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.