Problem/Motivation

The Drupal API hook hook_entity_view_mode_alter() does not work when page manager is enabled.

Steps to reproduce

  1. Create panels based site with Panel everywhere + Page manager (node_view page) + panelizer
  2. Panelize Full page override view mode with panelizer
  3. Create custom Mobile view mode.
    /**
     *  Implements hook_entity_info_alter().
     */
    function custom_entity_info_alter(&$entity_info) {
      $entity_info['node']['view modes']['mobile'] = array(
        'label' => t('Mobile'),
        'custom settings' => TRUE,
      );
    }
    
  4. Provide view mode switch on any conditions
    /**
     *  Implements hook_entity_view_mode_alter().
     */
    function custom_entity_view_mode_alter(&$view_mode, $context) {
      $browser = browscap_get_browser();
      if ($browser['ismobiledevice'] === "true" && $context['entity_type'] == 'node' && $view_mode == 'full') {
        $view_mode = 'mobile';
      }
    }
    
  5. View node in Mobile view mode.
  6. Node will be render using Full page override view mode.

Proposed resolution

Figure out a way for hook_entity_view_mode_alter() to operate as expected.

Remaining tasks

  1. Write Patch

User interface changes

None.

API changes

The Drupal API hook hook_entity_view_mode_alter() may start to function where previously it did not.

Data model changes

None.

Comments

niko-’s picture

Issue summary: View changes
niko-’s picture

Patch attached

niko-’s picture

Status: Active » Needs review
andypost’s picture

+++ b/plugins/task_handlers/panelizer_node.inc
@@ -283,19 +283,21 @@ function panelizer_panelizer_task_render($handler, $base_contexts, $args, $test
+  $panelizer_view_mode = (isset($context->data->panelizer_view_mode) ? $context->data->panelizer_view_mode : 'page_manager');

extra brackets is not needed

niko-’s picture

Hi Andy,

In case if panelizer use Full page override view mode $context->data->panelizer_view_mode key not present.
So this shorthand required.

niko-’s picture

Updated patch

niko-’s picture

andypost’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/plugins/task_handlers/panelizer_node.inc
@@ -283,19 +283,22 @@ function panelizer_panelizer_task_render($handler, $base_contexts, $args, $test
-  if (empty($context->data->panelizer['page_manager'])) {
+  // Use context view mode if available otherwise try page manager.
+  $panelizer_view_mode = isset($context->data->panelizer_view_mode) ? $context->data->panelizer_view_mode : 'page_manager';
...
+  if (empty($context->data->panelizer[$panelizer_view_mode])) {

great!

damienmckenna’s picture

Just out of interest, shouldn't this be set for all entities instead of just nodes?

niko-’s picture

I will check this case for taxonomy and user during next few days and update issue with info.

damienmckenna’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

Leaving it as "postponed" until you check other entities. Thanks.

davidwbarratt’s picture

Status: Postponed (maintainer needs more info) » Needs work

I put the "full page" in panalizer for node, taxonomy, and user and did something like this:

function test_entity_view_mode_alter(&$view_mode, $context) {
  drupal_set_message($view_mode);
  $view_mode = 'teaser';
}

What I got was:

Node
page_manager
Taxonomy Term
full
User
full

I applied the patch and that didn't change anything.

I also noticed that the view mode did not change to "teaser" on nodes like I expected (with or without the patch). The "teaser" that I have is not in Panalizer.

davidwbarratt’s picture

Title: Custom view mode incompatibility if page manager enabled » hook_entity_view_mode_alter() does not work if page manager enabled
Issue summary: View changes
davidwbarratt’s picture

Title: hook_entity_view_mode_alter() does not work if page manager enabled » hook_entity_view_mode_alter() does not work if page manager is enabled
Issue summary: View changes
davidwbarratt’s picture

Status: Needs work » Needs review
StatusFileSize
new2.15 KB

Attached is a patch that fixes the issue.

Status: Needs review » Needs work
davidwbarratt’s picture

#15 is a patch against the stable version, here's a patch against the dev version.

davidwbarratt’s picture

Status: Needs work » Needs review
davidwbarratt’s picture

roam2345’s picture

this needs to be ported to drupal 8