Drupal 7 - Creating a template suggestion for a node's view mode

February 13, 2015

After watching this video, the relationship between nodes, views, and view modes really started clicking for me. I decided to try and provide template suggestions for all my node types and their view modes. This simple code in template.php did the trick.

[php] function THEME_preprocess_node(&$variables, $hook) { $variables['theme_hook_suggestions'][] = 'node__' . $variables['type'] . '__' . $variables['view_mode']; } [/php]

Obviously you'll want to replace THEME with your theme's machine name. What this does, for every node type is create a template suggestion. So if there was a node type / content type for Programs in a typical University website and you created an "illustrated_list" view type to display programs in a list, a template suggestion called node__program__illustrated_list would be automatically created for you. Creating a file called node--program--illustrated-list.tpl.php would allow you to create the custom styling every time that view mode is used on that node. Wonderful!

Relevant StackExchange thread