Closed (fixed)
Project:
Display Suite
Version:
7.x-1.3
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
7 Nov 2011 at 13:06 UTC
Updated:
18 Feb 2013 at 19:49 UTC
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
Comment #1
swentel commentedThe 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 :)
Comment #2
swentel commentedNode, 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.
Comment #3
rudiedirkx commentedI would expect it's possible to change a node's
view_modesomewhere 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)?Comment #4
swentel commentedThis is the core patch #1154382: View mode no longer can be changed
Comment #6
duaelfrIf 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
Comment #7
charlie-s commentedThe new hook: http://api.drupal.org/api/drupal/modules%21system%21system.api.php/funct...
(provided as of Drupal 7.17)
Comment #8
duaelfrIt seems that the
node_showfunction 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
Comment #9
rudiedirkx commentedThat's enough?node_showcallsnode_view_multiplewhich callsnode_viewwhich usesnode_view_alterandentity_view_alter.node_show>node_view_multiple>node_view>node_build_content->hook_entity_view_mode_alterComment #10
duaelfrOh nice !
I will implement this hook in my module asap thank you for the information !