Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#1 | field_collection-hook_entity_view_mode_alter-2019299-1.patch | 1.7 KB | steven.wichers |
field_collection-hook_entity_view_mode_alter.patch | 1.62 KB | steven.wichers | |
Comments
Comment #1
steven.wichers CreditAttribution: steven.wichers commentedAnd 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.
Comment #2
steven.wichers CreditAttribution: steven.wichers commentedI just found out that you can get the host entity from the field collection entity by using 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.
Comment #3
rrrob CreditAttribution: rrrob commentedI have tested the original patch and it works great for me.
Comment #4
rrrob CreditAttribution: rrrob commentedComment #5
fagoMakes 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.
Comment #6
fagoI've implemented this generically and committed it - thus all entities based on the EntityAPIController will receive the hook as well then.
Comment #8
johnheaven CreditAttribution: johnheaven commentedEDIT - 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.
Comment #9
johnheaven CreditAttribution: johnheaven commented