Problem/Motivation

Discovered in #1889798-22: Panelizer support (use Panels' render pipeline to re-render edited field panes in Panels).

Panelizer has "panelizer view modes", similar to "entity view modes". However, they're independent of "entity view modes": you can configure them inside Panelizer, not in the Entity API UI.

Panelizer, when rendering entity field panes, then essentially calls field_view_field() with its display settings ($display = array(…)), not a view mode ($display = 'teaser'). This effectively means that in HOOK_preprocess_field(), $variables['element']['#view_mode'] === '_custom_display'. And there is no way to retrieve the panelizer view mode that is being applied, this is the problem.
At least, that's impossible AFAICT. If it is, show me how and you can close this issue right away.

Proposed resolution

My work-around for now is this:

/**
 * Implements hook_panelizer_pre_render_alter().
 *
 * Panelizer fails to communicate to the theme layer what view mode an entity's
 * fields are being rendered in, so we unfortunately have to do that ourselves.
 */
function panelizer_panelizer_pre_render_alter(&$panelizer, &$display, &$entity) {
  foreach ($entity->panelizer as $view_mode_key => $reference_panelizer) {
    if ($reference_panelizer === $panelizer) {
      $entity->panelizer_view_mode_key = $view_mode_key;
      break;
    }
  }
}

With this in place, I can get the "panelizer view mode" in HOOK_preprocess_field() via $variables['element']['#object']->panelizer_view_mode_key.

Remaining tasks

TBD

User interface changes

None.

API changes

TBD

CommentFileSizeAuthor
#3 panelizer-n2267601-3.patch863 bytesdamienmckenna

Comments

damienmckenna’s picture

Any reason to not just call the attribute "panelizer_view_mode"?

damienmckenna’s picture

FTR I don't see field_view_field() being called anywhere by Panelizer, it's only called by CTools' entity_field plugin - should the burden instead be on CTools to insert the view mode, when applicable?

On another note, is it safe to do the object comparison "if ($reference_panelizer === $panelizer)" for all supported releases of PHP? Might it be instead safer to compare the actual Panelizer settings to ensure they're the same?

damienmckenna’s picture

StatusFileSize
new863 bytes

The code above made available as a patch.

damienmckenna’s picture

Status: Active » Needs review
wim leers’s picture

Any reason to not just call the attribute "panelizer_view_mode"?

Nope, that's fine :) I just wanted to be explicit that it is the identifier, not the corresponding object.

damienmckenna’s picture

Status: Needs review » Fixed

I tested this out as part of testing #1889798: Panelizer support (use Panels' render pipeline to re-render edited field panes in Panels) and it worked just fine. Thanks Wim!

  • Commit 83c5df4 on 7.x-3.x authored by Wim Leers, committed by DamienMcKenna:
    Issue #2267601 by Wim Leers, DamienMcKenna: Added a new variable (...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.