In #2379145: Panopoly Magic live preview does not render field collections, we added support for rendering live previews of FPP's that include field_collection fields. With field_collection 7.x-1.1, this now longer works, and generates some PHP notices:

Notice: Undefined index: value in field_collection_entity_preload() (line 942 of /profiles/openberkeley/modules/contrib/field_collection/field_collection.module).
Notice: Undefined index: value in field_collection_entity_preload() (line 948 of /profiles/openberkeley/modules/contrib/field_collection/field_collection.module).
Notice: Undefined index: value in field_collection_entity_preload() (line 948 of /profiles/openberkeley/modules/contrib/field_collection/field_collection.module).
Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityAPIController->load() (line 219 of /profiles/openberkeley/modules/contrib/entity/includes/entity.controller.inc).
Notice: Undefined index: value in field_collection_entity_preload() (line 963 of /profiles/openberkeley/modules/contrib/field_collection/field_collection.module).

This is due to the new field_collection_entity_preload() function which is run by the new field_collection_field_formatter_prepare_view() hook implementation.

I'm not totally sure what the correct solution is here...

Comments

dsnopek created an issue. See original summary.

dsnopek’s picture

StatusFileSize
new736 bytes

Here's a patch to field_collection that'll make it start working again. However, I'm not sure this will be acceptable to the upstream maintainers, because under normal conditions this code would never be necessary.

dsnopek’s picture

An idea that might be acceptable, is rather than the patch in #2, the patch to field_collection could look like:

diff --git a/field_collection.module b/field_collection.module
index 8e1b80925..946d27296 100644
--- a/field_collection.module
+++ b/field_collection.module
@@ -938,6 +938,11 @@ function field_collection_entity_preload($entities, $langcode, &$items, $fields)
   foreach ($entities as $id => $entity) {
     foreach ($items[$id] as $delta => $item) {
 
+      // Check if the entity already exists.
+      if (isset($item['field_collection'])) {
+        continue;
+      }
+
       // Check if this item is in our local entity cache
       if (isset($local_entity_cache[$item['value']])) {
         $items[$id][$delta]['field_collection'] = $local_entity_cache[$item['value']];

... and then we could patch panopoly_magic.module to set both the 'entity' and 'field_collection' keys.

The reason I think that might be acceptable, even though it wouldn't happen under normal circumstances either, is that it's at least internally consistent. You're not left wondering, "where is this 'entity' key coming from?" you see that this code is loading entities into the 'field_collection' key and this code is just skipping that if it's already set.

Unfortunately, I don't really have any better ideas at the moment, but I'll stew on this for a little bit...

dsnopek’s picture

I've been trying to think of other ideas that might work as well...

One is to add a new field formatter for field collections that works just like the normal one but is compatible with panopoly_magic's preview. However, sites will need a configuration change in order to work (and most will probably not even know this is something they need to do) and we're still susceptible to changes in field_collection meaning we need to update this new field formatter.

Another is to use hook_field_formatter_info_alter() to change the module owner of the field formatter to panopoly_magic and then replace all the implementation hooks with our own versions that'll work. But this still has the weakness that it couple's our new implementation hooks to updates in the field_collection module (and we could break field_collection in live display of items too, not just preview).

So, patching field_collection upstream might be the only way here...

dsnopek’s picture

Status: Active » Needs review
StatusFileSize
new691 bytes

I've made a field_collection issue with an upstream patch here:

#3111102: No longer working with live previews in Panopoly

That needs to be paired with this panopoly_magic patch. I think it'd probably be OK to commit this to panopoly_magic before the field_collection one is commited, because who knows how long that could take, and that would mean you'd only need to apply a single patch to get this working again.

cboyden’s picture

Status: Needs review » Reviewed & tested by the community

This is working on our Panopoly child distribution - also commented on the field_collection issue.

  • dsnopek committed 4a04971 on 7.x-1.x
    Issue #3108161 by dsnopek, cboyden: panopoly_magic doesn't render...
dsnopek’s picture

Status: Reviewed & tested by the community » Fixed

There's no harm in committing this on our end, even if field_collection doesn't commit the patch there. So, committing!

Status: Fixed » Closed (fixed)

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