I need nodes within a certain place in my menu to use a new node TPL file.

My menu is hierarchical like this.


I need nodes whose menu link is link3, linkA or linkB to use these new TPL files.

I have 2 content types whose nodes can be in the menu and I need a different TPL file for each content type. These nodes can also be outside of the menu in which case they should use the normal TPL file.

So a node of content type A which has a menu link of link1, link2 or no menu link would use the default node--typeA.tpl.php.

A node of content type A which has a menu link of link3, linkA or linkB would use node--typeA--special.tpl.php

A node of content type B which has a menu link of link1, link2 or no menu link would use the default node--typeB.tpl.php

A node of content type B which has a menu link of link3, linkA or linkB would use node--typeB--special.tpl.php

As I'm extending the node not page TPL I started with this:

function MYTHEME_preprocess_node(&$variables) {
  $variables['theme_hook_suggestions'][] = 'node__SOMETHING';

Im assuming I need an if statement around the 2nd line which checks if the node is typeA and also in the correct place in the menu? However I had a look at the $variables on the screen and I couldn't see anything I could use to find out the menu position.

I then looked at preprocess_page to see if it had a variable I could use. There is an array called main_menu which is the name of my menu. But it contains 0 elements.

function MYTHEME_preprocess_page(&$variables) {
  • I guess my first question would be will this always and only be needed for nodes in the same position on this or any menu? It seems to me that though this case probably does not need a custom ct it may be a good place to use a taxonomy or even an extra field for the alternate tpl.
    – sareed
    Commented Apr 29, 2015 at 16:37
  • Its only this menu and its only for one top level link, and all its child links.
    – Evanss
    Commented Apr 29, 2015 at 16:39
  • @sareed what do you mean by "custom ct"?
    – Evanss
    Commented Apr 29, 2015 at 16:40
  • I meant a custom content type. I usually try to account for change when doing this type of thing. I would honestly suggest looking another direction. Like the taxonomy or alternate tpl field. That being said I will look into this because it is kind of interesting.
    – sareed
    Commented Apr 29, 2015 at 16:44
  • The pages within this section will be different content types, and these content types are already used elsewhere so it could cause a lot of duplication to create additional 'page', 'video', etc type nodes just for this.
    – Evanss
    Commented Apr 29, 2015 at 16:46

2 Answers 2


As this requirement may get even more complex, I would suggest adding a taxonomy selection to the content types in question. You can use this to assign the proper theme suggestions as the value will be saved on the node object. You may also want to check out ThemeKey.

  • This may be the only solution but the downside is this is risky in the long term as editors must always set the menu link AND the taxonomy. If they do only one or the other the styling will be wrong.
    – Evanss
    Commented Apr 29, 2015 at 17:22
  • That is true but you can use Conditional Fields to mitigate that. Note that you can also use #states if you don't want to load a module for this one instance.
    – sareed
    Commented Apr 29, 2015 at 19:25
  • I don't think that module would work for me. Fields are conditional on other fiends. I would need a field to be conditional on a particular menu link. Even then the field would be available for editing but the editor would still need to set the value.
    – Evanss
    Commented Apr 29, 2015 at 20:38
  • I might be able to use Rules or a custom module to set a taxonomy term based on a menu link. But this feels a bit heavy handed to me.
    – Evanss
    Commented Apr 29, 2015 at 20:39

It might not be ideal but ive solved it by checking the URL:

function MYTHEME_preprocess_node(&$variables) {

  // Get the node URL
  $nodeUrl = $variables['node_url'];

  // Break up the string 
  $urlParts = explode("/", $nodeUrl);

  // Get the first part of the string
  $urlBegin = $urlParts['1'];

  // Get the node type
  $nodeType = $variables['type'];

  // If the ULR starts with 'CUSTOMURL'
  if ($urlBegin === 'CUSTOMURL') {
    // Add theme hook suggestions based on node type as well as URL
    $variables['theme_hook_suggestions'][] = 'node__' . $nodeType . '__CUSTOMURL';

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.