This is like a follow up of Required nested IEF forms don't work.
In that issue, there was a resolution for a nested IEF when there was a required IEF field and the page was submitted without clicking the confirmation button on the IEF form.

Though in the previous field the solution worked, it did not fix the issue for the existing entities.
I am returning with the same issue but for a different button.
Steps to reproduce:

  • Enable IEF
  • Create two content types A and B
  • On entity A, create a entity reference field referencing to content type B and mark it as required
  • Set the form view for the reference field in content type A to be a complex IEF form that allowes to add new entities
  • Create an entity of type B
  • Go to the node/add page for entity type A
  • Fill in the title as it is required
  • In the IEF form press "Add existing"
  • Select the existing entity of content type B
  • Do NOT press the "Add " confirmation button in the IEF form
  • Submit the parent form
  • Expected behavior: The entity is saved with the reference
  • Actual result:Submission fails with message 'This value should not be null'

Comments

idimopoulos created an issue. See original summary.

dimilias’s picture

This is only the failing test.

claudiu.cristea’s picture

Status: Active » Needs review

Let's trigger the test.

Status: Needs review » Needs work
dimilias’s picture

I have created a small patch which is a first approach that seem to fix the problem though I haven't tested it in nested forms and the failing test above is also not for a nested case.

I found out that after the ticket that I refer at the top, there was a @todo added referring to the unimplemented part of the existing entities.
For this part, I used a simplified version of the Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete static method and the Drupal\Core\Entity\Element\EntityAutocomplete::matchEntityByTitle protected static method which are used for the normal autocomplete field.
It seems to solve the issue though I might have missed something. And I have tried it for both selecting from autocomplete and for selecting by label.
The label does is not included in the test but other tests were failing when it was not ready.

dimilias’s picture

Status: Needs work » Needs review
dimilias’s picture

Update: I haven't written a test for this but in a manual test I created 3 content types A, B, C.
The A has a reference to B and the B to C.
In the forms, A and B are using the IEF to refer to B and C respectively, both of which are mandatory fields.
There is an existing entity of C type.
I went to the create form of A and through the IEF I pressed create new entity (B).
In the nested IEF of B I pressed the "Add existing" button.
I filled in everything properly. Everything worked like charm :)

claudiu.cristea’s picture

Assigned: Unassigned » claudiu.cristea

Assigning for review.

sandervd’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @idimopoulos,

Works like a charm!

pfrenssen’s picture

Assigned: claudiu.cristea » Unassigned
Status: Reviewed & tested by the community » Needs work

Found a couple of nits:

  1. +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
    @@ -566,12 +568,69 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
    +      else if ($widget_state['form'] == 'ief_add_existing') {
    

    According to the coding standards, elseif should be written as one word. It is also a good practice to use === instead of == when doing string comparisons.

  2. +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
    @@ -566,12 +568,69 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
    +          $options = array(
    

    Let's use the shorthand array syntax [].

    This can be applied throughout the whole patch.

  3. +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
    @@ -566,12 +568,69 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
    +          /** @var /Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler */
    

    The first forward slash should be a backslash.

  4. +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
    @@ -566,12 +568,69 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
    +          $handler = \Drupal::service('plugin.manager.entity_reference_selection')
    

    Let's inject this service rather than sideloading it.

alunyov’s picture

Status: Needs work » Needs review
StatusFileSize
new9.23 KB

Adjusted code. Replaced loading the service statically with dependency injection. Please review it.

dunebl’s picture

#11 is doing the job.
Many thanks alunyov!!

darvanen’s picture

+++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
@@ -566,12 +580,68 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
+              elseif (count($entities) > 5) {
...
+              elseif (count($entities) > 1) {

As per #10.1 and Drupal coding standards, these need to be two words: else if () {.

Otherwise the patch works well, and should be RTBC when that last nit is fixed. Please remember to include an interdiff.

darvanen’s picture

Status: Needs review » Needs work
alunyov’s picture

Status: Needs work » Reviewed & tested by the community

@Darvanen,
I'm sorry, but I didn't get your point:

As per #10.1 and Drupal coding standards, these need to be two words: else if () {

According to codding standards it should be elseif (like it is in code now). Even if you check link you are referencing Control Structures it says:

(Note: Don't use "else if" -- always use elseif.)

Getting the ticket back to RTBC.

darvanen’s picture

Wow, I totally had that backwards, sorry.

It wasn't RTBC when I commented but I agree it is now.

dunebl’s picture

I think the patch needs to be rerolled:

patching file README
patching file src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
Hunk #2 FAILED at 40.
Hunk #5 succeeded at 605 (offset 32 lines).
1 out of 5 hunks FAILED -- saving rejects to file src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php.rej
patching file src/Tests/ComplexWidgetWebTest.php
Hunk #1 succeeded at 430 (offset -2 lines).
darvanen’s picture

Queued for re-testing against 8.6

Status: Reviewed & tested by the community » Needs work
kaythay’s picture

Status: Needs work » Needs review
StatusFileSize
new9.15 KB

Just needed a minor modification to get it working. Please review!

Status: Needs review » Needs work

The last submitted patch, 20: 2830328-required-ief-existing-entity-reroll.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

darvanen’s picture

Thanks @kathay, can we have an interdiff next time please?

Looks like it's failing, but at least it's applying!

kaythay’s picture

Looks like a test function was replaced between #11 and #20. Updated the patch with interdiff against #1.

kaythay’s picture

Status: Needs work » Needs review
darvanen’s picture

Sorry @kaythay the interdiff we need is against #11 - the most recently reviewed patch. I'm struggling to find the difference between that and your patches except for the line numbers.

kaythay’s picture

StatusFileSize
new2.06 KB

Sorry about that. #11 was actually not a full patch and only applied against #2 so I had only interdiffed against #2. Here is the interdiff against #11.

darvanen’s picture

I think perhaps that hunk didn't apply when you went to create the interdiff because both patches contain that section.

What I'm taking from this is that your reroll of the patch didn't change any code, just the line numbers in the patches (which was necessary, obviously because #11 wasn't applying properly.

Is that correct? If you didn't change any code, this is RTBC.

kaythay’s picture

The only change besides line number was changing $this->setAllowExisting(TRUE); to $this->updateSetting('allow_existing', TRUE); in #23 for tests to pass.

darvanen’s picture

Status: Needs review » Reviewed & tested by the community

Great! Then I call #23 RTBC based on previous reviews.

andrey.troeglazov’s picture

StatusFileSize
new42.45 KB
new33.08 KB
new29.44 KB

Hello,
The #23 patch applied correctly and working as expected.
+1 for RTBC.
Tested on dev branch.

andrey.troeglazov’s picture

Issue tags: +IEF Release 8.x-1.0
jonathanshaw’s picture

jonathanshaw’s picture

knyshuk.vova’s picture

+1 to RTBC

  • kaythay committed d23131c on 8.x-1.x
    Issue #2830328 by idimopoulos, pfrenssen, alunyov, DuneBL, Darvanen,...
kaythay’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, everyone.

Status: Fixed » Closed (fixed)

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

mattlt’s picture

Hello,

Just updated from rc1 to rc2 and our inline_entity_form for existing entities started not working. Reverting back to rc1 got everything working again.

In rc2 when trying to select an existing image I would get the following error…

Notice: Undefined index: entity_id in Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex->extractFormValues() (line 621 of /modules/contrib/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php).

Please let me know if you need more information.

Thanks,

•• matt