Use different view mode based on theme
[PARTIAL RESOLUTION]
Both these are needed to work for teaser nodes in a view, at least:
<?php
/**
* Implements hook_entity_prepare_view().
*/
function feature_mobile_config_entity_prepare_view($entities, $type, $langcode) {
if ($type == 'node') {
global $theme;
if ($theme == FEATURE_MOBILE_CONFIG_THEME) {
foreach ($entities as $entity) {
if ($entity->view_mode == 'teaser') {
$entity->view_mode = 'mobile_teaser';
}
elseif ($entity->view_mode == 'full') {
$entity->view_mode = 'mobile_full_content';
}
}
}
}
}
/**
* Implements hook_node_view_alter().
*
* This successfully changes the view mode handed in to the renderable $build
* array, but note that it does not affect the build mode used earlier for
* field_attach_prepare_view() nor anywhere else in the prep before rendering.
*
* See <a href="http://data.agaric.com/use-different-view-mode-based-theme
" title="http://data.agaric.com/use-different-view-mode-based-theme
">http://data.agaric.com/use-different-view-mode-based-theme
</a> */
function feature_mobile_config_node_view_alter(&$build) {
global $theme;
if ($theme !== FEATURE_MOBILE_CONFIG_THEME) {
// We only act on the mobile theme, if not it, bail immediately.
return;
}
if ($build['#view_mode'] === 'teaser') {
$build['#view_mode'] = 'mobile_teaser';
}
elseif ($build['#view_mode'] === 'full') {
$build['#view_mode'] = 'mobile_full_content';
}
}
?>
For full nodes on a page alone, it seemed that the below was enough (but i sort of doubt it really was). Even with both approaches above, i'm afraid there are parts of the node building that is
<?php
/**
* Implements hook_node_view_alter().
*
* This successfully changes the view mode handed in to the renderable $build
* array, but note that it does not affect the build mode used earlier for
* field_attach_prepare_view() nor anywhere else in the prep before rendering.
*/
function feature_mobile_config_node_view_alter(&$build) {
if ($build['#view_mode'] == 'full') {
$build['#view_mode'] = 'mobile_full_content';
}
}
?>
Other Approaches & Background
The goal is to use a different view mode for nodes displayed in a view based on the active, default theme.
THEMENAME_preprocess_views_view() already has the nodes rendered: rows (String, 37383 characters ) <ul> <li class="row"><article clas...
<?php
function issymobile_preprocess_node(&$vars) {
$vars['view_mode'] = 'related_teaser';
$vars['entity_view_prepared'] = FALSE;
}
?>
Didn't work, even with the patch-- hoping for follow-up here: http://drupal.org/node/1154382#comment-5262600
Note also: hook_entity_prepare_view() cannot be implemented in a theme. Nothing happens.
See also, this works not in views but on standalone pages, not sure where/how to check for theme, actually theme is a global variable so that would be easy:
http://drupal.stackexchange.com/questions/10141/using-a-different-view-mode-with-a-node/13314#13314
Comments
Post new comment