I am using contextual filters on the /path/langcode view, where langcode is a contextual filter. How can I get langcode in a template file?


You can get the contextual filter argument from the route match:

  $variables['arg_0'] = \Drupal::routeMatch()->getParameter('arg_0');

Usually the route parameters of contextual filters are named arg_0, arg_1 etc. If it doesn't work, check the route definition of this View page with Devel (/devel/routes) or Drupal Console.

Dependent on the Twig template you might need to add a cache context as well:

  $variables['#cache']['contexts'][] = 'route';

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.