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
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | panelizer-n2267601-3.patch | 863 bytes | damienmckenna |
Comments
Comment #1
damienmckennaAny reason to not just call the attribute "panelizer_view_mode"?
Comment #2
damienmckennaFTR 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?
Comment #3
damienmckennaThe code above made available as a patch.
Comment #4
damienmckennaComment #5
wim leersNope, that's fine :) I just wanted to be explicit that it is the identifier, not the corresponding object.
Comment #6
damienmckennaI 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!