0

In Drupal-specific Twig template I can get current path like

{% set pagePath = path('<current>') %}

But such a way doesn't contain query string e.g. ?param=value if the latter is present in the path

Is there a "built-in" way in Drupal-specific Twig to get full path including query string?

Under "built-in" I mean "without preprocessing it in a PHP code", e.g. in a module or in the themename.theme file with PHP code

Note on possible duplicates

I am not sure this is a duplicate of https://stackoverflow.com/a/20329384 as Drupal has its own specific Twig syntax - unfortunately that Symfony-specific option doesn't work here.

halfer
  • 19,824
  • 17
  • 99
  • 186
stkuser
  • 51
  • 6
  • DarkBee, why do you shorten the title? It's a question specific to Drupal + Twig and not "how to get current path" in general – stkuser Oct 26 '22 at 16:00
  • There is no need to put tags in the title – DarkBee Oct 26 '22 at 16:01
  • @DarkBee: I agree with the prevailing guidance on "detagging" titles, but "How in Drupal-specific Twig template to get current path with query string?" would not normally be considered taggy. An example of a taggy title would be "Drupal Twig How can I get current path with query string in a template?" Sometimes the tags are bracketed, sometimes they are not, but either way, they aren't very clear, and are trying to duplicate the tag system. – halfer Oct 26 '22 at 18:59
  • FWIW, I think [my Meta post here](https://meta.stackoverflow.com/a/253076) is the current canonical advice on detagging titles. In other words, if a title can be (re)written so that key words are naturally-occurring words that make grammatical sense, those key words do not need removing. – halfer Oct 26 '22 at 19:00
  • The only "vanilla" way I see, is that you pass the `request` instance to template to fetch the querystring. e.g. `path('', request.query.all())` - Also keep in mind that there is a full SE dedicated to [drupal](https://drupal.stackexchange.com). Maybe you will get an answer faster there – DarkBee Oct 27 '22 at 05:41
  • @DarkBee This throws the error `"Argument 3 passed to Drupal\Core\Routing\UrlGenerator::processRoute() must be of the type array, null given ..."`. Even if I specify empty array as the third argument – stkuser Oct 27 '22 at 09:00
  • How did you defined the "empty" array? with `{}` or `[]`? – DarkBee Oct 27 '22 at 09:17
  • I'm trying `{% set params = [] %}{{ path('', request.query.all(), params) }}` But the braces throw the error as well – stkuser Oct 27 '22 at 10:37

0 Answers0