Currently it is very difficult to programmatically change the view mode of a field collection entity when it is rendered as part of a node. This was true of entities in general until Drupal added hook_entity_view_mode_alter.

This patch adds a drupal_alter prior to the field collection render process.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

steven.wichers’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1.7 KB

And here's a patch so you have the entity that contains the field collection. Useful if you need to change the view mode based on settings of that entity.

Don't use this patch. See comment 2.

steven.wichers’s picture

I just found out that you can get the host entity from the field collection entity by using hostEntity().

function MYMODULE_entity_view_mode_alter(&$view_mode, $context) {
  $host_entity = $context['entity']->hostEntity();
}

This means the second patch provides redundant functionality. The major difference being an explicit definition of the host entity instead of relying on someone to be aware that it's accessible via the existing entity wrapper. Just a heads up to anyone who is planning on using on of the patches. The first is enough.

rrrob’s picture

I have tested the original patch and it works great for me.

rrrob’s picture

Status: Needs review » Reviewed & tested by the community
fago’s picture

Title: Implement hook_entity_view_mode_alter(). » Add support for hook_entity_view_mode_alter().
Project: Field collection » Entity API
Version: 7.x-1.0-beta5 » 7.x-1.x-dev
Component: Code » Entity CRUD controller
Status: Needs review » Needs work

Makes sense. But as rendering field collection item entities is implemented via the entity api controller, I think the fix should go in there. Thus moving.

fago’s picture

Issue summary: View changes
Status: Needs work » Fixed

I've implemented this generically and committed it - thus all entities based on the EntityAPIController will receive the hook as well then.

Status: Fixed » Closed (fixed)

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

johnheaven’s picture

EDIT - just realised I made a big mistake! This is a diff of my whole development environment with loads of test code in there, not just the affected folder. So don't use it!

I tried this patch, but had to apply it manually. I tried rerolling it - hope it's ok as I haven't rerolled a patch before ... it looks quite long, hope I haven't done it wrong! But it works for me.

johnheaven’s picture