0

I'm trying to get an argument from search page URL so that I can pass that argument to a link. For example I have this URL for a search results page: http://domain.com/site-search?search_api_fulltext=foo And on that page I want to have a link that goes to: http://domain.com/other-search?search_api_fulltext=foo

I've tried {{ url('<current>') }} but that only returns the domain and path, but not the arguments. I've been trying stuff like {{ app.request.requestUri }} but that returns nothing.

FWIW, I'm trying to include these links in a custom block that appears on the search results page.

3
  • 1
    app. is for symfony apps. You could preprocess this variable and pass it in. – Kevin Dec 6 '19 at 18:37
  • I'm trying that with: function MYTHEME_preprocess_block(&$variables) { $current_path = \Drupal::service('path.current')->getPath(); $variables['current_path'] = $current_path; } But its still not returning the arguments, just the page path. – fallenturtle Dec 6 '19 at 18:58
  • Ah, I figured out the function: function rcorpta_preprocess_block(&$variables) { $current_path = \Drupal::request()->getRequestUri(); $variables['current_path'] = $current_path; } – fallenturtle Dec 6 '19 at 19:04
-1

well if you are making a custom block then you are probably doing something like this ....

 $build = [];
 $build['#theme'] = 'your_template',
 return $build;

and then in you hook_theme().

 hook_theme() {
  return [
   'your_template' => [
     'render element' => 'children',
     'template' = 'your-templante-name',
    ],
  ];
 }

you don't need to pre prossess the block at all just make sure the block is not being cached aka add this to your block plugin.

see How to prevent a block from being cached?

public function getCacheMaxAge() {
  return 0;
}

then in your block function :

 $search_query = \Drupal::request()->get('search_api_fulltext');
 $build = [];
 $build['#theme'] = 'your_template',
 $build['#search_query'] =  $search_query,
 return $build;
....


 hook_theme() {
  return [
   'your_template' => [
     'render element' => 'children',
     'template' = 'your-templante-name',
     'variables' => [
       'search_query' => NULL,
     ],
    ],
  ];
 }

then in your twig template

{% if search_query %}
  do what you want here {{search_query}}
{% endif%}
1
  • Thanks. I'm going to try this later on. I did figure out how to do it in a way I'm not happy about where I used in my theme file: ``` function THEME_preprocess_block(&$variables) { $current_path = \Drupal::request()->getRequestUri(); $variables['current_path'] = $current_path; } ``` And then in my twig: {% set search_string = current_path|split('=')|last %} But I want to come back and try your suggestion as I want everything in the custom module and not part of the theme. – fallenturtle Dec 9 '19 at 18:46

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.