Score:0

Add theme suggestion to a node depending on the menu it belongs to

fj flag

I have a mega menu that displays child menu items when you click on a parent menu item in form of a drop-down menu. The thing is that, this menu, also displays cards as nodes and I want to add a theme suggestion only to this card nodes that are inside the menu drop-down.

I can get the node with a simple MYTHEME_theme_suggestions_node_alter() hook but I don't know how to get the menu item of it.

I've tried menu_ui_get_menu_link_defaults($node) as seen in this question but it always returns 'main', which is correct but, also almost every node on the page apart from the drop-down menu node seems to come from it and, therefore, that's not what I want.

All I need is an exclusive theme suggestion for this nodes represented on the mega menu drop-down

cn flag
Why not just create a new view mode (e.g. menu_card), and render the nodes in that view mode? It'd be cleaner that way plus you get the standard `node__menu_card` suggestion already waiting for you
leymannx avatar
ne flag
Yeah, display the nodes in that menu in a new view mode and you immediately have the template suggestion.
ng flag
Agree with the other commenters, new view mode is the way to go.
Score:0
fj flag

Adding a new view mode just for this types of nodes that are rendered on the menu, is the best approach, specially if this nodes are just from one bundle like 'page'.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.