I have created two custom templates (header-before-login.html.twig and header-after-login.html.twig). I would like to get data from the database to pass to these templates using variables.

How can I overwrite this templates as preprocess in xyz.theme?

These are the paths of the files.

  • drupal/docroot/profiles/custom/project/themes/xyz/xyz.theme
  • drupal/docroot/profiles/custom/project/themes/xyz/templates/header-before-login.html.twig
  • drupal/docroot/profiles/custom/project/themes/xyz/templates/header-after-login.html.twig
  • 1
    function xyz_preprocess_header_before_login(array &$variables) – user21641 Feb 3 '18 at 10:58
  • I tried. It's not working. In header.html.twig. I'm including both this templates. If logged in then calling after-login template elase before-login template. I even tried with function xyz_preprocess_header(array &$variables) & cleared cache as well. Still not working – Julia Feb 3 '18 at 13:21
  • How are those templates called? Is it really a template that you call from PHP or is it just a twig include, then you can't preprocess it. – Berdir Feb 3 '18 at 21:04
  • It's just twig include. Not called from PHP. Now we are actually on point. I think – Julia Feb 4 '18 at 2:59
  • Yes, that's not a template as far as Drupal is concerned, there's no preprocess or anything like that. I guess you should be able to implement preprocess of the template that includes them and provide the variables there, but you might need to pass them through to the other, don't know how that works in detail in twig. – Berdir Feb 5 '18 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.