If using the normal 'Content' rows display plugin (on node views) that comes with the Views module (views/modules/node/views_plugin_row_node_view.inc), it has the following line in the plugin's render function:

$node->view = $this->view;

This is very useful as we can access it in hook_node_view(), template_preprocess_node(), etc.

However, when using the 'Rendered entity' row display, the View object isn't available on $node. In my particular case, I can't use the 'Content' display as the row style as I've a search api search index view. And before you ask, the particular manipulation of the node that I need to do in hook_node_view() or the theme layer, can't be done easily through a view mode - I need the View context.

Anyway, the attached patch adds in support for this.

Comments

stella’s picture

Here's another version of the patch as I missed making the same change to views/handlers/entity_views_handler_field_entity.inc for the 'show complete entity' field display handler.

alcroito’s picture

Issue summary: View changes
Priority: Normal » Major
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new1.8 KB

This is a very important patch to have the context of the view in entity_view, so you can extract various fields in hook_entity_view, without doing additional queries and thus impacting performance.

Re-rolled the patch against latest HEAD.

Also reviewed, and it works!

fago’s picture

Status: Reviewed & tested by the community » Needs work

While this migth come handy, it is bad-practice to throw arbitrary stuff on the entity object. If you want to keep something for later I'd suggest setting it in the render array. Also, this could use a comment why we are doing it.

lwalley’s picture

Status: Needs work » Needs review
StatusFileSize
new1.54 KB

Added comments as suggested. I agree this is clumsy, but I'm not sure how else to pass the view object through to the render process so that it can be referenced during render of entity fields?