When fired from field_view_field, then the context does not contain anything useful regarding the view mode:
// Invoke hook_field_attach_view_alter() to let other modules alter the
// renderable array, as in a full field_attach_view() execution.
$context = array(
'entity_type' => $entity_type,
'entity' => $entity,
'view_mode' => '_custom',
);
The patch adds a 'display' key and passes through the $display passed to the function (with defaults filled in). Easy! The semantics of this is "can be either the name of a view mode or an array of display settings" so for consistency we can pass in view_mode in field_attach_view and keep the semantics. Consistency++
Test shows one use case. The real life use case was a bit more complicated: i was rendering entity type A which related to entity type B and both had the same field attached and I needed the field value from B instead of A in some cases and was standing there baffled when I saw _custom.
This patch largely obsoletes the view_mode key/value in the context array but it's too late to change that and luckily the argument to field_view_field is called display and not view_mode so that's a good excuse too why added a new one instead of changing view_mode...
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 990148-5.patch | 4.86 KB | jhodgdon |
| field_attach_view_alter.patch | 3.82 KB | chx |
Comments
Comment #1
moshe weitzman commentedHarmless and useful.
Comment #2
yched commentedsimilar to #946696: field_view_field() calls hook_field_attach_view() inconsistently - got to run, no time to compare patches :-(
Comment #3
chx commentedThat's a whole another issue. It involves the alter hook but it's about the incoming $output misses '#entity_type' and '#bundle' properties
and the $context misses a 'language' entry. This is a completely different issue -- it's about view mode / display.
Edit: that issue does not add a new key, this one does. It also misses tests.
Comment #4
jhodgdonThe doc on this needs a bit of editing. New patch coming in a few minutes.
Comment #5
jhodgdonOK, here's a new patch. Only the docblocks were changed from chx's patch above.
Comment #6
chx commentedThat's great thanks
Comment #7
webchickThis is just adding additional data that can be futzed with in an alter hook and doesn't break anything. Comes with tests to ensure it works as expected.
Looks good to me, but it'd be great to have final +1 from yched.
Comment #8
yched commentedSorry, spent the afternoon between meetings, I jumped the gun a little fast in #2 - different issue indeed -
#5 is fine by me.
Comment #9
webchickCommitted to HEAD. Thanks.