1

We have a header which contains a secondary menu on top bar, search, main menu and logo in page.html.twig.

We would like to implement a new header with quite a few changes and so would like to add a bool field or some field, somewhere for example all Content Types. So the user, adds a page sees this field and if checked to display new header. Is this possible?

3
  • 1
    The answer is: yes. I would probably do this with a custom block and then use blockAccess or hook_block_access to hide it given the current node's checkbox value. drupal.stackexchange.com/questions/203308/…
    – leymannx
    Oct 14 at 15:41
  • But you could also do this in hook_preprocess_page where you check if the current route is node canonical, get the node, get the value and set a custom variable you then can if-else in the page template.
    – leymannx
    Oct 14 at 15:42
  • Thank you, I am complete newbie to Drupal. I cannot change the current header to block, it is directly implemented in page.twig.html. It is new header I can work with.
    – Melonye
    Oct 14 at 16:49
1

You can actually access nodes directly from page.html.twig.

It's not as clean as a preprocess approach, but you could do something like this:

{% if node is not empty %}
  {% if node.field_some_field is not empty and node.field_some_field.value == true %}
    {# Output new header #}
  {% endif %}
{% endif %}

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.