11

How do I retrieve the query parameters from a path. Here's my path:

/search?keys=hello

Here is what I've tried:

$test1 = \Drupal::request()->request->get('name');
$test2 = \Drupal::request()->query->get('name');
$test3 = \Drupal::service('path.current')->getPath();
$test4 = \Drupal::request()->server->get('REQUEST_METHOD');
$test5 = \Drupal::request()->query->get('q');
$test6 = \Drupal::request()->request->get('keys');
$test7 = \Drupal::request()->query->get('keys');
$test8 = \Drupal::request()->query->get('destination');
$test9 = \Drupal::request()->request->all();
$test10 = \Drupal::request()->getRequestUri();

Here are the results:

Test 1: NULL

Test 2: NULL

Test 3: '/search'

Test 4: 'GET'

Test 5: NULL

Test 6: NULL

Test 7: NULL

Test 8: NULL

Test 9: array ( )

Test 10: '/search'

Why is this not working?

  • 1
    \Drupal::request()->query->all() – user21641 Mar 24 '17 at 16:15
11

The problem was the block was cached. See Clive's comment:

public function build() {
  $test = \Drupal::request()->query->get('keys');
  return array(
    '#markup' => $test,
    '#cache' => array(
      'contexts' => array(
        'url.query_args:keys',
      ),
    ),
  );
}
| improve this answer | |
9

This is a duplicate. If you look at the change log, there's a beautiful example for how to retrieve $_GET and $_POST variables.

I have tested locally at /search?keys=hello and

\Drupal::request()->query->get('keys');

works, as well as

\Drupal::request()->get('keys');

I tested your custom block and it works fine. Have you tried clearing cache?

Update:

If it is caching, you can set your block to not cache, since it's a search block. Or you can set it to cache for each set of parameters. Thanks @clive for the link.

| improve this answer | |
  • 1
    I'm running the code from inside a custom block. Maybe the block gets cached, and my code does not run? – dbj44 Mar 24 '17 at 15:56
  • 1
    Yes, I have tested your code and it's working fine for me. Do you know how to clear the cache? – oknate Mar 24 '17 at 17:01
  • 1
    Definitely @24ma13wg, see drupal.stackexchange.com/questions/231953/get-in-drupal-8/… for an example to fix it – Clive Mar 24 '17 at 17:02
7

You were close. All you need to do is get:

$keys = \Drupal::request()->get('keys');

Here is an example of me using it:

/**
 * Implements hook_preprocess_views_view()
 * @param $variables
 */
function mytheme_preprocess_views_view(&$variables) {
  if ($variables['id'] == 'acquia_search' && $variables['display_id'] == 'results') {
    $variables['get']['keywords'] = Xss::filter(\Drupal::request()->get('keywords'));
    $variables['get']['sort_by'] = Xss::filter(\Drupal::request()->get('sort_by'));
    $variables['get']['sort_order'] = Xss::filter(\Drupal::request()->get('sort_order'));

    if (!Unicode::strlen($variables['get']['sort_by'])) {
      $variables['get']['sort_by'] = 'search_api_relevance';
    }

    if (!Unicode::strlen($variables['get']['sort_order'])) {
      $variables['get']['sort_order'] = 'DESC';
    }
  }
}
| improve this answer | |
  • 1
    I get NULL for that? – dbj44 Mar 24 '17 at 15:26

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.