The languagevariable is added to the twig variables by default but it is empty for my custom module which is rendering a page.

I tried adding theme_preprocess_page hook inside the *.module file but the variable is already set there...

Do I have to add it manually through the routes file or is there any alternative?

  • Any page template includes the language variable and this is retrieved from the language manager independently from your module. So probably you are rendering a template other than page. Can you provide more details?
    – 4uk4
    Commented Oct 8, 2017 at 11:40
  • I'm rendering a custom template by defining it inside the module file and referencing it from controller like return ['#theme' => 'my_page', ...]. So this is not a page in Drupal terms?
    – madpoet
    Commented Oct 8, 2017 at 12:11
  • No, this is simply a custom template. You can name it like you want, but including "page" in the name doesn't make it a page template. The preprocess hook would be in your case module/theme_preprocess_my_page where you can add the language variable like core does this for the page template.
    – 4uk4
    Commented Oct 8, 2017 at 12:39
  • ... though in your case you can directly provide the language variable from the controller, a preprocess hook is only necessary if you want to change the output of other modules.
    – 4uk4
    Commented Oct 8, 2017 at 12:47
  • I get it now, that explains the other issues I'm having thanks... Do you know any way to tell Drupal to render it as a "page"? The "page"s can be modified in themes and I'd want the custom template I'm rendering to match the customizations in the theme...
    – madpoet
    Commented Oct 8, 2017 at 12:52


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.