I have created a fieldable panel pane with several fields. One of the fields on the pane is a field collection.

When creating a new instance or editing an instance of the FPP inside of panels IPE, the fields that are part of the field collection do not render in the preview.

here's a better explanation with pictures:

1. outlined in red is one panel pane. in green is the field that is part of a field collection (there is only one field in the collection at this point, but there will be more)

2. after choosing "customize this page" then clicking the gear icon on the FPP that i wish to edit, i get the edit screen overlay with the preview showing. Initially, the field that is part of the field collection shows in the preview

3. after changing the content of "url" at the bottom, the preview refreshes, but the content from that field is now missing from the preview (for those who are wondering, if i edit the "textbox" field which is not a field collection, it refreshes correctly in the preview)

4. after hitting save on the edit overlay, i am returned to my page. the field collection field data is not visible on the FPP that i just edited.

5. after clicking save at the bottom of the page, everything renders as expected and the missing data re-appears.

If someone with more experience in this module has an idea on where the issue is coming in, i would be happy to work on the fix and submit a patch. my debugging so far has gotten me to the obvious place where the ajax call happens: function panopoly_magic_ajax_form_callback(), but where to go from there i am not too sure.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dsnopek’s picture

Thanks for the bug report, with really great steps to reproduce! Unfortunately, I still haven't had a chance to dig into this. However, I suspect that the patch in this issue will have an affect (could be positive or negative) on field collections in the live preview:

#2398347: Panopoly Magic abuses FPP revisions

If you have the time, could you try using that patch and seeing if it helps?

maskedjellybean’s picture

The above patch does not help. I'm using Panopoly version 7.x-1.28 and am experiencing this bug.

I've tracked the fieldable_panel_pane entity object around panopoly_magic.module and found a function call that takes the object as an argument (with all fields containing their proper values) and then returns the object except with my Field Collection field set to an empty array. This is the source of the problem as the object no longer contains any values for the Field Collection field but is then used to render the preview. The call is to field_attach_submit() on line 371 inside panopoly_magic_form_alter().

After this point things are a little too deep for me to grasp. I know field_attach_submit is a part of the core Field module but I don't quite understand what it does. Does this mean this issue is outside of Panopoly's control? If so, how does the Field Collection module not run into this issue elsewhere?

dsnopek’s picture

I can confirm what @maskedjellybean says - the field_attach_submit() is removing the field values from the entity. However, removing that doesn't automatically fix things.

The problem is that the field formatter is unable to get the field_collection_item entity in order to view it. It's field_collection_field_get_entity() that's responsible for getting it from the field item, and it first tries to pull the entity off of $item['entity'].

So, I think if we could somehow detect that a field collection is in use, and then makes sure the field items contain the fully built out field_collection_item entities, we should be able to get this to work. We will need to prevent field_attach_submit() from clearing out the values, though.

dsnopek’s picture

I validated that setting the $item['entity'] value would work by hacking up panopoly_magic with some code specific to the field I was testing. Making that more generic and having it run for any fields we detected were using field_collection is an option, but it'll be tricky because we can't conditionally remove the field_attach_submit() - that runs for all fields on the entity at once.

So, I dug more and found where field_collection is going wrong in the first place. In field_collection_field_widget_embed_validate() it's got a conditional block at the end:

  // Only if the form is being submitted, finish the collection entity and
  // prepare it for saving.
  if ($form_state['submitted'] && !form_get_errors()) {
    field_attach_submit('field_collection_item', $field_collection_item, $element, $form_state);

    // Load initial form values into $item, so any other form values below the
    // same parents are kept.
    $item = drupal_array_get_nested_value($form_state['values'], $element['#parents']);

    // Set the _weight if it is a multiple field.
    if (isset($element['_weight']) && ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED)) {
      $item['_weight'] = $element['_weight']['#value'];
    }

    // Put the field collection item in $item['entity'], so it is saved with
    // the host entity via hook_field_presave() / field API if it is not empty.
    // @see field_collection_field_presave()
    $item['entity'] = $field_collection_item;
    form_set_value($element, $item, $form_state);
  }

This gets the field's items into the right format - the problem is that $form_state['submitted'] isn't TRUE when generating the preview, I think because we're using the AJAX system. I'm not sure if there is an earlier place we can force it to be TRUE? Or if we're going to have to come up with a way to repeat this logic in panopoly_magic?

dsnopek’s picture

Status: Active » Needs work
FileSize
444 bytes

I forgot that we are using our own custom AJAX page callback! So, we can set $form_state['submitted'] = TRUE before the element validation runs.

I've attached super simple patch that almost works.

However, it has the following problems:

  1. It saves the fields on the field collection when the preview updates. So, if you make a change, but don't click "Save" and only close the dialog, and reload the page, then your temporary changes persist
  2. It acts kind of strange on new FPPs - the preview doesn't appear until you add a 2nd item, at least in my testing, which might be a little specific because I'm using an image field and have lots of required fields

So, we probably don't want to run the submit stuff and just temporarily setup the field items just before they are viewed?

dsnopek’s picture

Status: Needs work » Needs review
FileSize
2.94 KB

Here's a patch without the weaknesses of the previous patch! It doesn't save on preview, and it works as expected with new FPPs. Unfortunately, it requires direct support for field_collection, but we have a history for putting specific support for contrib modules in panopoly_magic.

dsnopek’s picture

FileSize
3.03 KB
855 bytes

Here's a tiny change to this patch. I was experiencing a problem where we couldn't access the host entity when the preview was rendering the field collection items (which we could access during normal rendering). This fixes that in my minimal testing!

dsnopek’s picture

Status: Needs review » Reviewed & tested by the community

This has been in use on a client site for a while without major incident. It's also very clearly looking for field_collection fields specifically, so it shouldn't have negative effects on sites that don't use it, and they are already experiencing problems with field_collections, so... I'm going to run this through our test suite and if it passes, I'm going to merge it!

Here's the Travis build: https://travis-ci.org/panopoly/panopoly/builds/188951458

  • dsnopek committed 389a74c on 7.x-1.x
    Update Panopoly Magic for Issue #2379145 by dsnopek: Panopoly Magic live...
dsnopek’s picture

Status: Reviewed & tested by the community » Fixed

Committed!

Status: Fixed » Closed (fixed)

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