0

I want to create a custom tpl for a specific node. However I dont want to use node id in the file name (page--node--11.tpl.php). Is there any way to do it?

1

Sure, you can use hook_preprocess_node() or hook_preprocess_page() to suggest any template you want for a node or a page. I am suggesting both because of the name you gave in your question page--node--11.tpl.php is really a page template not a node template.

Regardless, you could do this:

function YOURTHEME_OR_MODULE_preprocess_node(&$variables) { 

  if ( /* whatever logic */ ) {

    $variables['theme_hook_suggestions'][] = 'node_whatever_you_want_to_call_it';

  }

}

or

function YOURTHEME_OR_MODULE_preprocess_page(&$variables) { 

  if ( /* whatever logic */ ) {

    $variables['theme_hook_suggestions'][] = 'page_whatever_you_want_to_call_it';

  }

}

and now when that node is rendered, it will use node-whatever-you-want-to-call-it.tpl.php or if the page, page-whatever-you-want-to-call-it.tpl.php.

0

Check for any attributes you are using to determine if it is the correct node in template_preprocess_page (in your themes template.php) and then add to the $variables['theme_hook_suggestions'] array.

Something like this:

function YOUR_THEME_preprocess_page(&$variables) {
    if(isset($variables['node']) && $variables['node']->your_field_name[LANGUAGE_NONE][0]['value']=='some_value') {
        $variables['theme_hook_suggestions'][] = 'page__your_template';
    }
}
  • fyi, if you are on node/###, $variables['node'] will already contain the node object, so there isn't much need to node_load() it again (even though it is most probably still cached). – Jimajamma Feb 20 '14 at 16:42
  • Thanks Jimajamma - I was wondering if there was a node object hiding away in somewhere but couldn't see it - I'll remember that as is useful to know. – Felix Eve Feb 20 '14 at 16:50
  • Quite welcome; I find printing out array_keys($variables) to be quite helpful in finding out what you have to work in these kinds of drupal hooks. – Jimajamma Feb 20 '14 at 16:56
  • I was using dmp() but I think the problem lay with my eyes not the code! – Felix Eve Feb 20 '14 at 16:59

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.