Specify a template for referenced nodes I was wondering how to specify a specific template for rendering a node when it's referenced from another content type.For example I have a content-type (product) and I use the node--product.tpl.php template to render nodes of this type. Then I have a content-type named container that have a node reference field that may related a number of product nodes.I would like to use a different template when rendering a product when the product is referenced from the container content-type.
I just installed the theme developer module and check suggested templates, I then created a new template named node-reference--product.tpl.php in the same folder as node--product.tpl.php but this file is only used if node--product.tpl.php is removed.
I guess one approach would be to use some kind of theme_preprocess hook and load a node based on the arg(1) value and then check if the type is container and if it is add a template suggestion in the $variables parameter.
The simplest thing you can do is provide a new "View Mode" for nodes, so that you can use it with the "Rendered Node" display option for the node reference field.

This would be done in a small custom module using hook_entity_info_alter() like so:

function MYMODULE_entity_info_alter(&amp;$info) {
  $info['node']['view modes']['node_reference_product'] = array(
    'label' =&gt; 'Node Reference Product',
    'custom settings' =&gt; TRUE
  );
}


Once the module is installed you'll have a new view mode under the "Rendered Node" display format options:



Now you can simply use the same node--product.tpl.php template file as normal and just test for the view mode to define what you want to display:

&lt;?php if ($view_mode == 'full') : ?&gt;

  &lt;!-- HTML output for the full view mode --&gt;

&lt;?php elseif ($view_mode == 'teaser') : ?&gt;

  &lt;!-- HTML output for the teaser view mode --&gt;

&lt;?php elseif ($view_mode == 'node_reference_product') : ?&gt;

  &lt;!-- HTML output for your new view mode --&gt;

&lt;?php endif; ?&gt;


Of course you still have access to the view mode in any hook_preprocess_node function so you could put your logic in there instead:

function MYTHEME_preprocess_node(&amp;$vars) {
  if ($vars['view_mode'] == 'product_node_reference') {
    $vars['content'] = array('#markup' =&gt; 'Some other content');
  }
}


But perhaps the best part of solving your problem this way is that Drupal honours the new view mode in the UI, so for each content type you will now have a new view mode which you can choose options for as normal:



Just like the standard full and teaser view modes you will be able to decide which fields are displayed, and the ordering and format in which they are displayed too. It's entirely possible that after writing that tiny one-function module you won't need to write another line of code to solve the problem.

You'll also have this view mode available system wide for use in, for example, Views/Panels etc.