My goal is to switch between different view modes based on the node path (pattern). In my case, the patterns would be:
- dossier/node-alias => default template
- dossier/node-alias/cover => cover template
- dossier/node-alias/abstract => abstract template
I have created the custom view modes with hook_entity_info_alter
(simplified here to only one custom view mode):
/**
* Implements hook_entity_info_alter
*/
function dossier_config_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['cover'] = array(
'label' => t('Cover view mode'),
'custom settings' => TRUE,
);
}
Here is how I declared the path pattern in hook_menu
:
/**
* Implements hook_menu
*/
function dossier_config_menu() {
$items = array();
// Cover entity view mode
$items['dossier/%/cover'] = array(
'title' => 'Default title', // overridden in template.php preprocess_node function
'page callback' => '_dossier_config_switch_viewmodes',
'page arguments' => array(1, 2), // arguments from uri are 1 => node alias and 2 => cover
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
And here is my custom callback:
function _dossier_config_switch_viewmodes($alias, $view_mode) {
// Get the node path based on the alias
$path = drupal_lookup_path('source', 'dossier/' . $alias);
// Return the node if the path exists
if ($path !== FALSE) {
$node = menu_get_object("node", 1, $path);
$node_view = node_view(node_load($node->nid), $view_mode);
return array(
'content' => array(
'node' => $node_view,
),
);
}
return drupal_not_found();
}
It works. However, when looking at these nodes in the custom view modes, I lose the node edition tabs (View, Edit, Translate, etc.) and I also seem to lose all the standard classes that are added to the <body>
such as node-type-xxx
or page-node-12345
etc.
I believe that this is a normal behavior with such a custom callback in hook_menu
(?). So what would be the right way to achieve what I want?
Any help on that will be highly appreciated! Cheers!
Edit: I forgot to mention that of course I have 3 different node templates and that I have added the theme_hook_suggestions
accordingly in my theme template.php
.
node/node-alias/view-mode
).dossier/node-alias/cover
where "dossier" is a path pattern that I defined for one particular content type, "node-alias" is my node alias and "cover" is the view mode to use. Hope this is clear enough :-)