User login

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

Searched words: 
use different view mode based on theme

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.