4

When Entity View Modes are defined, how does Drupal know which to use? Are they only useful with Views, or can they be used in the node display as well? If so, is that specified in the template, or somewhere else?

4

On the backend, the view mode is used everywhere node_view($node, $view_mode); is called. In general, the caller specifies which is needed. And since the system only knows about "full" and "teaser" by default, those are the ones that are baked into core. Code does have the option of reading all of the view modes when it wants to get rendered content.

Views are the most obvious place the view modes get used, and also the place I find the most helpful.

If you have a node reference, and set the display to "Rendered Node" you can choose the view mode, and it will pull from the list.

In Panels the view mode shows up in the settings for the "Node Being Viewed" content pane. This typically gets used in the node override page, and it the only way that I know of to specify the view mode for a node/$nid path.

I suspect there are other places, too, especially with modules that make use of CTools content types.

  • Is node_view something that would be used in the php template file, or from a module? – beth Sep 19 '12 at 20:38
  • It's mainly used by the templating system and by the search module (to get the page text). IIRC, the view mode gets passed around in the render tree as a parameter in normal circumstances. I have never directly called it before. Using it in your own template file is not a good idea. – mpdonadio Sep 19 '12 at 20:42
  • If it cannot be used in a template file, then how can it be used to affect the template output? – beth Sep 19 '12 at 21:20
  • Whatever is doing the templating (views, panels, etc) should be specifying the view mode. – mpdonadio Sep 19 '12 at 21:39
  • So If I'm not using either, just wanting to control the output of /node/xyz directly, is this not the right thing to use? – beth Sep 20 '12 at 13:00
5

With a tiny bit of glue code you can use specific node template files for different view modes, so they can become pretty useful:

function MYMODULE_preprocess_node(&$vars) {
  if($vars['view_mode'] == 'some_type') {
    $vars['theme_hook_suggestions'][] = 'node__' . $vars['node']->type . '__some_type';
  }
} 

That will allow you to use template files with names like node--article--some-type.tpl.php specifically targeted to that view mode.

Other than that MPD's answer has all of the other common cases covered.

  • 1
    Yeah, template suggestions w/ view modes is an awesome way to leverage their power. We typically bypass the checks and build up suggestions with $variables['theme_hook_suggestions'][] = 'node__' . $variables['view_mode']; and $variables['theme_hook_suggestions'][] = 'node__' . $variables['type'] . '__' . $variables['view_mode']; – mpdonadio Sep 19 '12 at 20:47

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.