0

I'm trying to add a variable that i can use in page.html.twig that will add a class to the header region if a field in the node exists.

In template_preprocess_page() i've tried doing the following and have not been able to add a variable:

function theme_preprocess_page(&$variables){
  $variables['header_attributes']['class'] = [];
  $variables['header_attributes']['class'] = array_merge($variables['header_attributes']['class'], ['myClassHere']);
}

I based this off the example for menu preprocess but no new class was added to the ta

2

There is no index called header_attributes inside $variables array no need to create it, there is page index just use it:
Try this out:

function [yourThemeName|yourModuleName]_preprocess_page(&$variables){
  $variables['page']['header_attributes']['class']= 'myClassHere';
}

Note don't forget to clear cache.
In your twig you will get your class by:

page['header_attributes']['class']
  • to implement template_preprocess_page(), in the function name theme_preprocess_page(), replace 'theme' with the name of your custom theme. Implement it in your theme's .theme file. Then clear cache – izus Oct 25 '18 at 21:49

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.