In inline_entity_form_field_widget_form() the $wrapper ID is based on the $instance['id']. This means that if 2 of the same field instance are placed onto the page, as they are in Commerce Registration during checkout, then the DOM has 2 elements with the same ID.

This causes IEF to load the inline form in the wrong place.

This issue is related:
#1740074: Fix IEF to work when IEF is nested into another IEF

Although that issue is related to a more specific goal of fixing nested IEF's.

Comments

tmsimont’s picture

Status: Active » Needs review
StatusFileSize
new1.65 KB

patch attach

bojanz’s picture

Status: Needs review » Closed (duplicate)

Let's continue in the other issue, since the issue is the same.

See my comment in http://drupal.org/node/1740074#comment-6710196 on how it should be done, I already said I don't want / like the "creating id from parents" approach (since the IEF id shows up in html classes, so it's just ugly.).

tmsimont’s picture

Status: Closed (duplicate) » Active

I am marking active again just to make a case about how this issue is in fact different than #1740074: Fix IEF to work when IEF is nested into another IEF

If you still disagree, just close it again.

The other issue is trying to address this issue, and 2 others in one place:

  • Nested IEFs do not execute inline_entity_form_save_row_weights so the correct order is never saved
  • inline_entity_form_process_entity_form is not executed on nested IEFs so subentities are not saved

Patching the module to fix #1740074: Fix IEF to work when IEF is nested into another IEF will be fairly time consuming whereas this issue is a simpler one, and can be addressed neatly and nicely here first. Then #1740074: Fix IEF to work when IEF is nested into another IEF can adapt to our solution here and move forward addressing those issues.

This issue also relates more closely to Commerce Registration.

I already said I don't want / like the "creating id from parents" approach (since the IEF id shows up in html classes, so it's just ugly.).

I understand how it could be "ugly" but this is how the element "name" values are already calculated in drupal. I got this concept directly from core's theme_form_element()

I suppose you could use something like php's uniqid(), but I would argue that a long string containing the parents of a form element is less "ugly" than a random string of numbers and characters.

We need an inline_entity_form_get_id function that returns a fresh id every time it's called (it can have a static variable that is incremented each time).
Then, use it across the code. Should solve all of our problems.

There won't really be any need to "use it across the code" -- I know the module currently generates the ID from the $instance param in 2 places, but if you review my initial patch, the second generation (@@ -1093,7 +1093,7, at the end of the initial patch) can actually be replaced by a retreival of the ID from the $form, rather than a new generation.

tmsimont’s picture

Attached is a patch that uses a incrementing static variable to generate a fresh ID for each IEF instance. This should fix this issue, but won't address the other problems in #1740074: Fix IEF to work when IEF is nested into another IEF

tmsimont’s picture

Status: Active » Needs review
jherencia’s picture

Status: Needs review » Needs work
+++ b/inline_entity_form.moduleundefined
@@ -769,6 +769,23 @@ function inline_entity_form_process_entity_form(&$entity_form, &$form_state) {
+  if (! isset($ief_domid) || empty($ief_domid)) {

There is a trailing space here.

tmsimont’s picture

better?

tmsimont’s picture

Status: Needs work » Needs review
jherencia’s picture

Yep :), I think this is an start to make both issues work but let's see what @bojanz thinks.

jherencia’s picture

I am thinking that for the wrapper and the #name properties it would be better to do str_replace('_', '-', $ief_id).

What do you think?

tmsimont’s picture

I'm not sure I understand how there would be a need for that (#10). If $ief_id is already generated cleanly in 1 place (with the patch in #7), why would you need to introduce str_replace('_', '-', $ief_id)?

tmsimont’s picture

if it's the dashes vs. underscores you're after -- i could just modify this line in the patch:

+ $ief_domid = 'ief_element_' . $ief_domid;

to be

+ $ief_domid = 'ief-element-' . $ief_domid;

jherencia’s picture

No, I'm saying that $ief_id is used to generate the ID of the wrapper div. The ID is using dashes and the $ief_id underscores. For the #name attributes happens the same, so it could be better to adapt the ID to conform those attributes.

tmsimont’s picture

So do you think the suggestion in #12 would work?

jherencia’s picture

We cannot use just one, there are cases in which the $ief_id should be used with underscores:

if (empty($form_state['inline_entity_form'][$ief_id])) {
  $form_state['inline_entity_form'][$ief_id] = array(

Code from the module

It's a Drupal standard to use undercore names for array keys.

I know this is probably the less important part of the patch but I think it is still important to keep the those standards.

tmsimont’s picture

I get it now, and agree. I'll send an updated patch today

tmsimont’s picture

OK attached is a patch for 7.x-1.x-dev that will do what the above patch did, then make all the "wrapper" elements point to a dash-separated name and let the form name parts use underscores.

I also fixed a comment:

@@ -780,7 +780,7 @@ function _inline_entity_form_get_new_id() {
     $ief_domid = 0;
   }
   $ief_domid++;
-  // Generate unique Map ID.
+  // Generate a unique ID.
   $ief_domid = 'ief_element_' . $ief_domid;
   return $ief_domid;
 }

It said "Map ID" because I copied and modified code from the getlocations module, which uses this to generate unique map id's

tmsimont’s picture

forgot to attach

bojanz’s picture

Status: Needs review » Needs work

The IEF id part is easy, the hard part is in inline_entity_form_field_attach_submit() -> processing the correct IEF from form state..
I can't just iterate over $form_state['inline_entity_form'] because that hook runs for inline forms as well, and I currently have no way to distinguish the two.
Nested IEFs make the problem even harder.

So this is not an easy thing to fix and will need to wait for another release.

tmsimont’s picture

Doesn't this part of the patch take care of that?

@@ -1093,7 +1110,7 @@ function inline_entity_form_field_attach_submit($parent_entity_type, $parent_ent
     if (isset($instance['widget']) && $instance['widget']['type'] == 'inline_entity_form') {
       $field_name = $instance['field_name'];
       $langcode = $form[$field_name]['#language'];
-      $ief_id = $instance['id'];
+      $ief_id = $form[$field_name][$langcode]['#ief_id'];
       // There's no IEF data for this field, skip it.
       if (empty($form_state['inline_entity_form'][$ief_id])) {
         continue;
-- 
msmithcti’s picture

@tmsimont thanks for this patch. The patch from #18 also works for multivalue field collections with inline entity forms within them.

tmsimont’s picture

no problem splatio.

@bojanz -- can you check out my comment in #20? I think we could mark this as RTBC with splatio's help.

fearlsgroove’s picture

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

Here's a rerolled patch that should still apply. This also seems to make nested entities work just fine in some light initial testing.

joe bartlett’s picture

The patch in #23 fixes the issue when applied to version 7.x-1.1 and adding the widget to a field collection (version7.x-1.0-beta5).

CSoft’s picture

#24

+1! Thanks, guys!

dww’s picture

I was pointed here via #1740074: Fix IEF to work when IEF is nested into another IEF. I haven't read the patch, but I just applied it and can definitively say it does *not* fix nested IEF in my case. :/

I've got a "Review" node type that has an entity reference called "Review by" that points to an "Author" node type, and an entity reference called "Reviewed" which points to a "Book" node type. In turn, Books have a "By" entity reference that also points to "Author". I've got all of the above ER fields configured to use IEF multiple values widgets.

When I'm creating a review node, I can use IEF to create or reference authors for the "Review by" field no problem.
I can also create or reference book nodes.

However, if I use IEF to create a new Author for the newly created book, my review node ends up with duplicate book nodes (although not with identical fields).

This is the same behavior with and without this patch. I'm using version 7.x-1.2.

If I have a chance to do a more thorough investigation I'll report back here, but either this patch isn't actually the solution to the deeper problem of nested IEF, or this patch is actually not solving the problem it wants to solve. ;)

Sorry I can't be more helpful right now...

Cheers,
-Derek

bendiy’s picture

Status: Needs review » Needs work

Marked #2046953: $ief_id isn't unique enough, thus doesn't work with field collection as a duplicate of this issue.
If #26 is the case then this needs more work.

amitaibu’s picture

Eventhough #2046953: $ief_id isn't unique enough, thus doesn't work with field collection was marked as the duplicate, I think the patch provided there is better.

bendiy’s picture

Attached is a rerolled patch of #23

bendiy’s picture

Status: Needs work » Closed (duplicate)

#2032649: Rework inline form submission solves this issue. Marking this as a dupe.

bendiy’s picture

Issue summary: View changes

found related issue