When Entity browser is used in IEF, cardinality defined on field is not propagated to Entity browser.

Way to reproduce (I have used EB Examples and also it requires IEF + EB IEF enabled):
1. on "Entity browser test" content type, change limit for "field_files" from unlimited to 2
2. for same field change form display widget -> to use Inline Complex
3. enable "Allow users to add existing file entities." and select same EB as it was used before.
--
4. create new "Entity browser test" content
5. choose "Add existing" and in EB select 3+ existing files
6. add them to IEF

=> all 3 will be added, button for adding new will be available (when you remove one file in IEF, button will be disappear, but as long it's N != Cardinality, button for adding new will be there).

I have found this issue during testing of: #2777747: [Needs tests] entity_form widget not updating after entity selection in entity_browser but it's not related with it. Fix from that issue just makes testing easier.

Comments

mtodor created an issue. See original summary.

slashrsm’s picture

Issue tags: +Novice

True. Should be pretty easy task. We need to read this setting from field storage and set it on entity browser form element.

marcoscano’s picture

Status: Active » Needs review
StatusFileSize
new1.15 KB

Is this the right place to add this?

Status: Needs review » Needs work

The last submitted patch, 3: propagate-field-cardinality-2778923-3.patch, failed testing.

The last submitted patch, 3: propagate-field-cardinality-2778923-3.patch, failed testing.

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new1.51 KB
new1.45 KB
slashrsm’s picture

Status: Needs review » Needs work

This belongs to entity_browser_entity_form_inline_entity_form_reference_form_alter().

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new769 bytes
new2.26 KB

Thanks for the feedback!

Another try, this time inside the form alter.

slashrsm’s picture

Status: Needs review » Needs work

Looks good. Just one thing.

     '#type' => 'entity_browser',
     '#entity_browser' => $entity_browser_id,
+    '#cardinality' => $instance->getFieldStorageDefinition()->get('cardinality'),
   ];

What if there are already entities selected? EB will still allow full number of entities, which will end up more than allowed. We should check how many entities are already selected and substract.

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new1.09 KB
new1.18 KB

True!
Attached another try. Not sure on how to fetch existing entities selected, though. This works, but feels kind of ugly

slashrsm’s picture

Status: Needs review » Needs work

IEF stores selected entities in the form state. You are able to get them with:

$form_state->get([
    'inline_entity_form',
    $reference_form['#ief_id'],
    'entities',
  ]);
marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new1.04 KB
new864 bytes

Much better! :)
Thanks!

slashrsm’s picture

Status: Needs review » Fixed

Committed. Thanks!

  • slashrsm committed 47af0fb on 8.x-1.x authored by marcoscano
    Issue #2778923 by marcoscano, slashrsm: Propagate field cardinality to...
ayalon’s picture

This patch break the entity browser if used together with inline entity form.

The problem is, that you can only subtract the number of entities if the cardinality is NOT -1.

With this patch, the validation will fail, if you add 2 new items in the entity browser and later try to add an existing one with the entity browser.

ayalon’s picture

StatusFileSize
new943 bytes

Status: Fixed » Closed (fixed)

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