1

In Drupal 8 I customize the filter in the block in the sidebar menu. When the user clicks on the option in the block, I need to pass his choice to the URL.

$this->args = \Drupal::request()->query->all();
$args[$id] = $value;
$url = $this->url = Url::fromRoute('<current>');
$url->setOptions(array('query' => $args));
$link = '<a href="' . $url->toString() . '">' . $label . '</a>';

And in the end, my URL looks like

 drupal/page_view?content_type=type

but I need my URL to look like

drupal/page_view?content_type%5Btype%5D=type

Do you know how can I modify my code?

| improve this question | | | | |
1

You try code :

$options = ['absolute' => TRUE];
$url = Url::fromRoute('entity.node.canonical', ['node' => 526,'destination'=>'router_back'], $options);
$link = Link::fromTextAndUrl('Here is the link', $url)->toString();

Result link:node/526?destination=router_back

You see more Url::fromRoute

| improve this answer | | | | |
1

Let's say if your $args is an array like below:

$args['content_type'] = 'type';

Then you have to append type to it rather than just giving your $id to $args

$args['content_type'] = ['type' => '123'];

Then your overall query will be like:

$this->args = \Drupal::request()->query->all();
$this->args = [$id => $value];
$url = $this->url = Url::fromRoute('<current>');
$url->setOptions(array('query' => $args));
$link = '<a href="' . $url->toString() . '">' . $label . '</a>';
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.