Can't add more than two items with simple inline entity form widget under some circumstances

Created on 18 October 2019, over 4 years ago
Updated 26 February 2024, 4 months ago

Problem/Motivation

On a site I'm working on I am unable to add more than two items to an inline entity form field. I have a form structured like this with several levels of nested entities:

-> The overarching node form
->-> Simple IEF pointing to an ECK-created smf_map entity type priority area bundle
->->-> Simple IEF pointing to smf_map shared outcomes bundle
->->->-> Simple IEF pointing to smf_map approach bundle
->->->->-> Simple IEF pointing to smf_map output indicator bundle
->->->->->-> Simple IEF pointing to smf_map output metric bundle

The process I go through is like this:

1. Open the node edit page
2. Select a priority area and see the shared outcomes section expand
3. Select a shared outcome and see the approach section expand
4. Select an approach and see the indicator section expand
5. Select an approach and see the metric section expand
6. Select a metric and expected value (so far so good)
7. Click the button to add another (new form added, two items, so far so good)
8. Click the button to add one more - no effect. The form does not change, it just shows the two I already added.

I at first thought this was due to the many nested entities, and it may be, but I have the same issue at any level of the tree so it's not specific to the furthest leaves at least.

This seems to be happening because Drupal\inline_entity_form\Plugin\Field\FieldWidget::formMultipleElements() removes the last item to prevent extra entity forms from being created but it also does it in some situations where it shouldn't. E.g. when a user intentionally added this item with the add more button or when saving the form when the element is not yet saved but does have input values.

Proposed resolution

I've been able to resolve this by adding some defensive code to Drupal\inline_entity_form\Plugin\Field\FieldWidget::formMultipleElements() to stop it from removing the last item in the following situations:

  1. When the triggering element is the add more button with a name matching the add more button for the currentl widget
  2. When there is corresponding form input for the widget

I'm not actually sure if this is an issue that would arise with a typical configuration (I've used this module many times before, though usually with the complex widget). It may be an incompatibility with, e.g., ECK. Unfortunately I don't have time to try and reproduce it with a more basic setup right now.

Remaining tasks

Test for the situation described in #3088901-26: Can't add more than two items with simple inline entity form widget under some circumstances β†’ .

πŸ› Bug report
Status

Needs work

Version

3.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada Dylan Donkersgoed London, Ontario

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom AndyF

    Thanks everyone for the time spent on this! I've been banging my head against the same underlying issue I think (I'm one of those using entity browser, eg. see #3226873: Entity reference field with cardinality unlimited - values are deleted when Simple is used β†’ which is closed as dupe for this one).

    This seems to be happening because Drupal\inline_entity_form\Plugin\Field\FieldWidget::formMultipleElements() removes the last item to prevent extra entity forms from being created but it also does it in some situations where it shouldn't. E.g. when a user intentionally added this item with the add more button or when saving the form when the element is not yet saved but does have input values.

    That's pretty much where I ended up, I think it looks like the items_count getting decremented causes the truncation.

    By comparing with the entity browser form array I've managed to get a fairly minimal test case: a button that doesn't trigger submit handlers. If you enable inline_entity_form_test, set it to multiple, and add the following in a form alter, it will cause data loss when pressed:

      $form['actions']['press_me'] = [
        '#type' => 'button',
        '#value' => t('Press me'),
      ];
    

    I've put together a (rough) test patch, it's late now so just posting as is, thanks!

  • The last submitted patch, 14: 3088901-14-TESTS-ONLY.patch, failed testing. View results β†’
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • πŸ‡¬πŸ‡§United Kingdom AndyF

    And here's the patch from #13 with the test from #14. Thanks!

  • πŸ‡¬πŸ‡§United Kingdom AndyF

    I don't know the IEF code very well, so I could easily be missing something (: But I was wondering if we could get by with just ensuring we don't decrement the delta if the form's being rebuilt. Here's a patch just to see what testbot has to say (and I've also improved comments on the test additions). There's no interdiff as it's an entirely different approach.

    Thanks!

  • Status changed to Fixed about 1 year ago
  • πŸ‡ΊπŸ‡¦Ukraine podarok Ukraine

    #17 is in
    tnx

  • πŸ‡¬πŸ‡·Greece GiorgosK Chios, Greece πŸ‡¬πŸ‡·

    #13 worked for inline_enitty_form 8.x-1.0-rc15

    problem was content type with entity reference to a product with variations could only hold 1 variation when saving the content
    any other variations was still in the database but the reference was lost from the product level

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

  • Status changed to Needs review 7 months ago
  • πŸ‡©πŸ‡ͺGermany geek-merlin Freiburg, Germany

    Bulk reopen.

  • πŸ‡¨πŸ‡±Chile mnico Santiago, Chile

    We have tested patch #16 on 3.0.0-rc19 and it works. It didn't work for us with patch #17.

    Regards

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.1 + Environment: PHP 8.1 & MySQL 8
    last update 4 months ago
    Patch Failed to Apply
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.1 + Environment: PHP 8.1 & MySQL 8
    last update 4 months ago
    Patch Failed to Apply
  • πŸ‡¬πŸ‡§United Kingdom AndyF

    Thanks for the feedback @mnico. Could you elaborate a little on how it's failing? Is it simply failing to make any difference at all to the behavior? Anything in the console? I tried locally using the test module against latest (c753ec180166fd9a12af9d9d047730d2ee2650b6) and the patch in #17 still seems to fix the issue where an ajax callback that doesn't trigger submit handlers can lead to data loss. Maybe there's another issue at play here?

    I tried to rerun the tests on #17 but the patch didn't apply, here's a reroll.

    Thanks!

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.1 + Environment: PHP 8.2 & MySQL 8
    last update 4 months ago
    29 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.1 + Environment: PHP 8.2 & MySQL 8
    last update 4 months ago
    25 pass, 1 fail
  • πŸ‡¨πŸ‡±Chile mnico Santiago, Chile

    Hello, to reproduce the issue a third level of references must be configured, an example is:

    1. Create 3 types of content: "First", "Second" and "Third" respectively.
    2. Create the Reference field in the content type "First" and point to the content type "Second" (unlimited). And configure the "Simple" Widget.
    3. Create the Reference field in the content type "Second" and point to the content type "Third" (unlimited). And configure the "Simple" Widget.

    I attach an example of the resulting form when creating content of type "First":

    With this configured, the bug will occur at the third level. If I apply patch #16 it fixes it, but not with #17.

    Regards

  • Status changed to Needs work 4 months ago
  • πŸ‡¬πŸ‡§United Kingdom AndyF

    Thanks for the detailed description @mnico. Moving to NW to catch #26 in a test.

Production build 0.69.0 2024