Is this really the right way to set the view mode? (I want to use a custom view mode for a node view when it's rendered through colorbox).

The below works, but it seems dirty to me..

$nid = $node->nid;
$node = node_load($nid);
$node = node_view($node, 'colorbox');
$page['content']['system_main']['nodes'][$nid] = $node;
print render($page['content']);

Note : unpredictable stuff happened when I rendered directly without $page. More specifically, I displayed a ctools comment form widget in the right ds region, and used a form_alter to redirect the comment submit to the same url (in the colorbox). Eventually the code above worked, but it just seems a bit odd.

Comments

swentel’s picture

Status: Active » Fixed

The assignment to the $page array seems weird indeed - although I probably need more context here.

However, calling node_view($node, 'view_mode_name'); is not dirty at all, it's really the way to go, no problem with that at all :)

swentel’s picture

Node, there's an issue in the Drupal core queue that will allow to dynamically change the view mode, because that's not possible anymore in D7, it was in D6. Hopefully that patch gets in fast so it will be easier in the future.

rudiedirkx’s picture

I would expect it's possible to change a node's view_mode somewhere before all the preprocesses and templates. (Or maybe inside a preprocess, like the node's.)

Putting the above code inside a tpl.php seems very wrong to me. Also 'manually' rendering a node's display seems off.

A node has a view_mode, right? Why can't you change it somewhere in the node without changing the tpl.php or rendering it yourself? Is that what the patch will do (and did in D6)?

swentel’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

duaelfr’s picture

If you need to define which view mode is used on the node/NID page I just released a little module.
Feel free to try it and to review it.

http://drupal.org/project/view_mode_per_role

charlie-s’s picture

duaelfr’s picture

It seems that the node_show function still call the "full" view_mode statically so I think this hook will need more love before being fully integrated.

Edit : the related core issue has been marked as in regression #1154382: View mode no longer can be changed

rudiedirkx’s picture

node_show calls node_view_multiple which calls node_view which uses node_view_alter and entity_view_alter. That's enough?

node_show > node_view_multiple > node_view > node_build_content ->hook_entity_view_mode_alter

duaelfr’s picture

Oh nice !
I will implement this hook in my module asap thank you for the information !