There is currently a number of open issues all around the same piece of code: inline form submission (inline_entity_form_process_entity_form()).

We run submission right after validation, in the same handler.
This causes ghost entities to be created when another form element (like the node title) triggers a validation fail.
It also doesn't with nested IEFs, and many other use cases.

Issues

#1643916: Product created without reference if Product Display has errors
#1740074: Fix IEF to work when IEF is nested into another IEF
#1934698: More than one required inline entity form on a page submits all required entity forms
#1660894: $entity_form[#op] does not reflect the operation actually being performed
#2016141: Fatal error on second form submission following validation error in host form when a field_collection field is on the ief
#1999464: Subfields not saving delta/order correctly.
I will mark them all as duplicate and test them once the new code is up.

Solution

We should introduce a custom #element_submit key that matches core's #element_validate one in behavior.
A custom submit handler will run all #element_submit functions.
The #element_submit functions will recurse, taking into account all IEF subforms.

Comments

arosboro’s picture

StatusFileSize
new3.9 KB

Hi bojanz,

I made a customizations to the ief id to resolve the issue in the short term. It may not be fully recursive, but it works for two levels of nesting according to my testing.

I have immediate needs for this functionality, and I'm maintaining two sites that require it. As such, I'll follow this thread and test any patches that come through. If I get some time, and nothing has been submitted, I'll give it another go.

bojanz’s picture

Thanks! I have an untested patch myself, I'll give both yours and mine a spin tomorrow night, and post something.

dww’s picture

Yay, this sounds great! I'll also be following and try to test/review ASAP.

Cheers,
-Derek

dww’s picture

Issue summary: View changes

Update summary

bojanz’s picture

Issue summary: View changes

Added another issue

cthos’s picture

Awesome, glad this is getting some traction.

tlindgren’s picture

I believe I have this same problem with a IEF fields getting duplicated when nested in a field collection. I'd be happy to nest when there's a new version. Thanks much.

arosboro’s picture

I've tested my patch in #1 to 3 levels of nesting and haven't found errors. It's a little hackish but it works for me.

roam2345’s picture

StatusFileSize
new4.3 KB

Just rerolled the patch. Was not working for me against latest 7.x-1.x branch.

$ patch -p1 < ief_nesting-2032649-1.patch
patching file inline_entity_form.module
Hunk #2 succeeded at 888 (offset 17 lines).
Hunk #3 succeeded at 1140 (offset 17 lines).
Hunk #4 succeeded at 1201 (offset 17 lines).
Hunk #5 FAILED at 1284.
bendiy’s picture

Status: Active » Needs review

This is working great for me. I've tested #7 to five levels of nesting and so far, it's working good from the UI Ajax side of things. I'll keep banging on it and do some full CRUD testing once I finish my submit handler.

I'm setting this to needs review. Are there any other outstanding change to be added?

I was originally trying #1835852: Multiple same-entity add/edit forms on one page results in duplicate DOM Id's, but this is working without using that patch. You can probably mark it as a duplicate as well.

arosboro’s picture

Thanks for re-rolling the patch!

nicholas.alipaz’s picture

Status: Needs review » Reviewed & tested by the community

#7 works for me, 2 levels deep, thanks!

bendiy’s picture

Reporting back regarding #8. This is still working great for me.

fearlsgroove’s picture

Status: Reviewed & tested by the community » Needs work

Using 1.3 with this patch, when using the multiple widget. If you have a row open and attempt to add a new entity, after hitting "create new entity," the new entity is not added to the list of existing entities.

bendiy’s picture

Status: Needs work » Needs review
StatusFileSize
new4.5 KB

@fearlsgroove
Please try the attached patch, it should address your issue.

bendiy’s picture

Issue summary: View changes

Remove issue

CSoft’s picture

Issue summary: View changes
Status: Needs review » Needs work

I apply any patch. If to use inline_entity_form inside field_collection, all items of field_collection are repeated with each other :(

bendiy’s picture

Status: Needs work » Needs review
StatusFileSize
new22.21 KB
bendiy’s picture

@CSoft
Can you provide some more details on your issue with field_collection?

  1. Steps to reproduce the problem
  2. Screenshot of the problem

I tried putting an Entity Reference field inside a Field Collection and using the Multiple Values widget. Everything is working as expected.

CSoft’s picture

CSoft’s picture

I'm sorry that I gave so little information about the problem. More details:

Field of field collection in node:

Machine name: field_product_bundles
Field type: Field collection
Widget: Embedded

Field field_product_bundles in field collections:

Machine name: field_product_bundle_products
Field type: Product reference (field of Drupal Commerce module)
Widget: Inline entity form - Multiple values

The problem occurs only with fields of type Product reference. They are repeated with each other, and only the one element are displayed, but elements of more than one.

I have attached a screenshots with the wrong display and with the correct:

Wrong:
Wrong

Correct (as should be):
Right

CosticaPuntaru’s picture

any news when this will be part of the official standard release?

checker’s picture

I tested both patches #13 and #7 but the error still exist. After there is an form error in the base entity the referenced entity is duplicated / exception (used drupal commerce product in node).

checker’s picture

This patch #2120817: inline_entity_form_process_entity_form() creates duplicate entities when validation error requires "Save" be clicked twice. fix my problem. First I thought it is a duplicate of this issue but it is not?

bendiy’s picture

@CosticaPuntaru We need to get #18 fixed first. I've been busy with other tasks. I should have some time in December, but if anyone else wants to submit a patch, please do.

bendiy’s picture

StatusFileSize
new26.38 KB
bendiy’s picture

@CSoft I think I've fixed #18 with my latest code at #2134035: Allow to add existing entities using the single value field widget. Either that or I can't reproduce it. This is what my field collection test looks like now:
field-collection-bug-fix

I'll post a patch in a few days.

bojanz’s picture

Sorry guys, I've been busy with other stuff lately. This is on top of my Inline Entity Form todo list.

bendiy’s picture

See the latest patch at #2134035: Allow to add existing entities using the single value field widget for a fix to this and some other issues. I'll see if I can find some time to break that up into smaller patches.

CSoft’s picture

bendiy, thanx, but after applying the patch, my problem remained unchanged :( All products of the first Field collection are repeated in the remaining Field collections...

The patch was applied on ief 7.x-1.3 and 7.x-1.x-dev.

bendiy’s picture

@CSoft Can you post a screenshot of your field_group setup. What you have have at:
/admin/structure/types/manage/page/fields
/admin/structure/field-collections/field_product_bundles/fields

I tried to recreate it, but did notice you have "Remove" buttons in your screenshots in #18 that mine doesn't have in #24. It looks like they're a level deeper than mine.

CSoft’s picture

CSoft’s picture

I have attached screenshots of the pages and screenshots with the field settings.

Commerce product
Commerce product

field_product_bundles
field_product_bundles

Field collection field_product_bundles
Field collection field_product_bundles

field_product_bundle_products
field_product_bundle_products

bendiy’s picture

@CSoft Thanks. I've recreated the bug on my system. I'll see if I can track down the cause.

bendiy’s picture

@CSoft It should be fixed now. Try the #9 patch at #2134035: Allow to add existing entities using the single value field widget.

I had to make sure the initial $child_delta knows about field_collection's form delta.

// Initial value.
else {
  // field_collections have a form delta that's needed to prevent duplicates.
  $child_delta = isset($form['#delta']) ? $form['#delta'] : 0;
}
CSoft’s picture

bendiy, thanks a lot! It's working perfectly for me! :)))

But I found two errors.

1) Notice: Undefined variable: new_entity in function inline_entity_form_settings() (line 312 in ...\sites\all\modules\inline_entity_form\inline_entity_form.module).

The error occurs when editing a product node.

2) Call to undefined function _field_widget_form() in ...\sites\all\modules\inline_entity_form\inline_entity_form.module on line 834

The error occurs when try to add an existing product on the button "Add existing product".

bendiy’s picture

@CSoft That's a know issue with Entity API. I've already reported it here: #2117637: Seemingly incompatible with other content types

bojanz pointed me here for a fix #1780646: entity_access() fails to check node type specific create access

bendiy’s picture

@CSoft Actually, I didn't reverse that commit correctly. See #10 patch at #2134035: Allow to add existing entities using the single value field widget

That will have the correct code, but you still need the Entity API patch.

CSoft’s picture

Thanks, the Entity API patch helped me.

The second error still appears. When debugging is seen that in this line of code

$existing_instance['widget']['module'] = $widget['settings']['type_settings']['existing_widget_settings']['module'];

the array has no 'existing_widget_settings' element:

Notice: Undefined index: existing_widget_settings in function inline_entity_form_field_widget_form() (line 825 in ...\sites\all\modules\inline_entity_form\inline_entity_form.module).

CSoft’s picture

I understood! It was necessary to re-save my ief field.

When selecting the type of widget "Add Existing Widget Type" -> "Autocomplete text field", this autocomplete field doesn't work and I get an error:

Notice: Undefined index: autocomplete_path in function commerce_product_reference_field_widget_form() (line 966 in ...\sites\all\modules\commerce\modules\product_reference\commerce_product_reference.module).

bendiy’s picture

@CSoft Thanks for testing. We should be getting close. Please try #11 over at #2134035: Allow to add existing entities using the single value field widget

That will fix your errors. Make sure and resave the widget settings.

CSoft’s picture

Great work! Thank you, it works! :)

Continue testing :)

CSoft’s picture

Oops, bendiy, try to remove any instance of the field collection using the "Remove" button.

bendiy’s picture

@CSoft The remove buttons are working for me. Both the collection remove and the IEF remove. What are you seeing?

CSoft’s picture

StatusFileSize
new57.8 KB
CSoft’s picture

I see:

Field collection removing.png

bendiy’s picture

CSoft’s picture

bendiy, thanks!

I found another problems.

1. Click "Add another item", add a product and press the "Add another item" button again. This product appears in the newly added field collection.

2. Press the "Add another item" button twice. Add a product to the first field collection. Try to add a product to the second field collection, it will be added to the first.

bendiy’s picture

@CSoft

The problem is that Field Collections doesn't attach an "item_id" for the row until you submit the main form. That item_id is the only unique thing in the field collection I can use to separate the different Field Collection rows in the Inline Entity Form. When you click "Add another item" two times in a row you end up with two undefined "item_id" rows and you see the problem you are having in #45.

The only why I can prevent that from happening is to force you to submit the main form before clicking "Add another item" the second time. You can do this through a form alter hook if you want, but I'm not sure Inline Entity Form should build in support for that use case. The problem really lies in processing Field Collections through ajax. Simply removing the "Add another item" button after the ajax call should work. Something like:

        // Remove the "Add another item" bottom from the field_collection form.
        unset($form_state['complete form'][$element['#field_parents'][0]][$langcode]['add_more']);

You could also add some processing to set the Field Collection "item_id" through ajax. That would need to be done outside of the Inline Entity Form module. I don't see any way to solve it inside Inline Entity Form.

The good news is everything seems to work as long as you submit the main form after you click Field Collection's "Add another item" button once.

bojanz’s picture

Please note that I won't be looking at #2134035: Allow to add existing entities using the single value field widget since it tries to fix many issues at once.
The patch I will be looking at is #13 from this issue. If you have newer fixes, reroll the patch.

bojanz’s picture

StatusFileSize
new13.99 KB

I looked at the existing patches and wasn't thrilled with what I saw. Hacky.

I sat down and implemented what I had in mind when I opened this issue.
Attaching today's progress. Still has bugs, so no point in testing it for now.
I'll roll another one in the morning.

bojanz’s picture

StatusFileSize
new18.69 KB

This one seems to be working fine.
Tested regular IEF forms (including submitting the parent form with a validation error such as an empty title), nested IEF forms (products referencing products, several levels deep).
Going to test field_collection now.

EDIT: Okay, found some more nesting bugs. Rerolling after lunch.

arosboro’s picture

Hi bojanz,

I see you've created a static counter for ief ids. Also, you made your custom submit handler.

I looked at the recursion in inline_entity_form_submit, and inline_entity_form_trigger_submit which determines which element to call this handler for. I see you've attempted to resolve the problem of submitting all forms by adding inline_entity_form_trigger_submit to the parent form's #submit array in inline_entity_submit_form_alter.

At line 1269, you can replace the if block with a call to form_execute_handlers. The $type argument should be ief_element_submit.

As far as being able to process all of the open forms, I think we need a method of keeping track of the forms and their hierarchy path in form_state. You would only need to call the open forms that exist after removing parents of any open form from your list.

Also, if '#op' == 'add' in the parent and a child form is open. Submitting the child entity and recursively calling the handler on it's parent will close that form on line 979. I suggest setting #ief_row_delta on the open parent add form, and replacing NULL with logic that will add the existing form with a new #ief_row_delta or NULL it out, closing the form of the triggering element. You can use #ief_row_delta in determining weight and instead of $form_state['inline_entity_form'][$ief_id]['entities'][] = array( you can do something like $form_state['inline_entity_form'][$ief_id]['entities'][$weight] = array(.

bojanz’s picture

Status: Needs review » Fixed

Committed the final version:
http://drupalcode.org/project/inline_entity_form.git/commitdiff/a754ba7?...

I've tested nested IEFs, and tested the IEF-inside-a-field-collection use case.
There are two bugs that I noticed in that field collection use case:
- Clicking the main submit button of the form won't submit open inline entity forms
(So if you clicked "Add product", if you didn't submit it by clicking "Create new product", it won't be created by submitting the node form)
This is a field_collection bug, they run the submission handlers during validation (just like IEF used to do before this commit), so our code hasn't had a chance to run yet.
- I needed to save the parent form after adding each field collection item, if I tried to manipulate the IEF right after clicking the add another button, the new field collection item didn't exist after save.

For any other bugs found please open new issues with precise reproduction instructions.

CSoft’s picture

bojanz, thanks! I observe a bug #43 in ief 7.x-1.x-dev. Do I need to create a new issue?

bojanz’s picture

Yes, please do.
Though my exploration shows that the bugs are now on the field_collection side. I will need to open some issues in their issue queue myself.

Status: Fixed » Closed (fixed)

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

ianthomas_uk’s picture

+++ b/inline_entity_form.module
@@ -1036,23 +1073,14 @@ function _inline_entity_form_autocomplete_validate($element, &$form_state, $form
-function inline_entity_form_process_reference_form(&$reference_form, &$form_state) {
-  // Only react on submissions triggered by the main submit buttons.
-  $triggering_element_name = end($form_state['triggering_element']['#array_parents']);
-  if (empty($form_state['triggering_element']['#ief_submit_all']) && $triggering_element_name != 'ief_reference_save') {
-    return;
-  }
-
+function inline_entity_form_reference_form_validate(&$reference_form, &$form_state) {

This change means that the inline entity form will now be validated even if it is not submitted by one of the main submit buttons. I'm not sure if that was intentional or not, but it has broken our usage.

We've got a dropdown that will reload the ief with different fields shown/hidden when it is changed. We want these fields to change whether or not the form currently passes validation.

We've been able to work around by checking $form_state['triggering_element'] in our validation function and skipping our validation when appropriate.

If the change was intentional, then I think it is worth explicitly mentioning in the release notes.

bojanz’s picture

Doesn't it respect #limit_validation_errors?
Validating on every submit but respecting #limit_validation_errors (allowing you to skip validation) would match core behavior for other elements.

ianthomas_uk’s picture

Thanks @bojanz, yes that works and is a better method than what I was doing before.

So, definitely not a bug, but still a change that could caused unexpected behaviour.

monaw’s picture

I'm eagerly awaiting the nested IEFT to work properly (:

The current version 7.x-1.5 shows nested IEF but all second level IEFs show up under all first level IEFs...

jnettik’s picture

So if I'm following this thread correctly I'm having the same issue.

My use is a store location with a pricing table. Locations are nodes with a reference to a "pricing table" entity created with ECK. The pricing table entity then references a "pricing table item" entity, also created with ECK.

The problem I'm having is I'll create the first pricing table for a location, and then go to add another pricing table. When I do the table items from the first table are already being referenced by the new item. Hopefully the map below illustrates that clearly:

Location 1
|- Pricing Table 1
|-- Pricing Table Item 1
|-- Pricing Table Item 1
|- Pricing Table 2
|-- Pricing Table Item 1 (auto referenced)
|-- Pricing Table Item 1 (auto referenced)

I'm using the dev branch with the same issue.

jnettik’s picture

My issue seems more related to #2206197: Nested Inline Entity form, issues on node creation with duplication of nested items, which the patch in #7 there fixes.