Hi there,

I've just found a "triple" bug within the IEF integration. The code in entity_browser_entity_form_inline_entity_form_reference_form_alter() currently breaks for having at least one of the 3 following use cases:

* The target entity type does not have bundle support: getTargetBundle() returns NULL in this case -> we have to use the target entity type ID instead
* The entity reference field is a base field -> you can't call $instance->get('cardinality') on base fields. But there's a nicer dedicated getCardinality() anyway, which can be used in both cases
* The entity form display is not stored in the database --> we have to follow the approach of the (deprecated) entity_get_form_display() function and create the form display on the fly

So, these are basically three different tiny issues, but I think it's easier to keep it in one issue report and patch.

I have already prepared a patch, and will also propose a PR on Github

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

agoradesign created an issue. See original summary.

agoradesign’s picture

so here's the patch

agoradesign’s picture

Status: Active » Needs review
ccyrille’s picture

j'ai le même problème avec la version 8.2. Le patch marche pour moi.
Thank!

ccyrille’s picture

Patch for 8.x-2.1

oknate’s picture

I believe it's possible to grab the form display from FormState storage:

+  $storage = $form_state->getStorage();
+
+  if (!empty($storage['form_display'])) {
+    $form_display = $storage['form_display'];
+  }

This will simplify the code a bit.

oknate’s picture

Version: 8.x-1.x-dev » 8.x-2.1
johnchque’s picture

Version: 8.x-2.1 » 8.x-2.x-dev

Bug reports should go always against the dev branch.

dwkitchen’s picture

Status: Needs review » Reviewed & tested by the community

I had all these three requirements in one use case

✅ Reference field was a base field
✅ Reference entity type was not bundled
✅ Form was not stored in the DB

All working as expected with the patch.

nuez’s picture

Status: Reviewed & tested by the community » Needs work

I think the patch in #6doesn't quite solve this issue, at least not for me.

+++ b/modules/entity_form/entity_browser_entity_form.module
@@ -26,11 +26,19 @@ function entity_browser_entity_form_inline_entity_form_reference_form_alter(&$re
+  $storage = $form_state->getStorage();
+
+  if (!empty($storage['form_display'])) {
+    $form_display = $storage['form_display'];
+  }

This won't work for multiple levels of embedded inline entity forms. The 'form_display' in storage applies to the root entity of the form.

+++ b/modules/entity_form/entity_browser_entity_form.module
@@ -26,11 +26,19 @@ function entity_browser_entity_form_inline_entity_form_reference_form_alter(&$re
+      'bundle' => $instance->getTargetBundle(),

This won't work for base fields, because the $instance variable might be of type BaseFieldDefinition, which doesn't have any information about the bundle in it.

Instead of looking at the field instance (base field or config field) we should look at the parent entity. However there seems to be no easy way to find the parent entity of the field. My suggestion is to propose a patch for the inline entity form that adds the 'parent' to the 'widget state' that is saved to the form_state storage by IEF, so we can use it here.

nuez’s picture

StatusFileSize
new4.94 KB

This patch needs to be used in combination with the patch provided in: https://www.drupal.org/project/inline_entity_form/issues/3253939

nuez’s picture

anul’s picture

Version: 8.x-2.x-dev » 8.x-2.6
Status: Postponed » Needs work

Last added patch/change works fine. We need to improve a code in terms to move this issue in Needs Review.

twod’s picture

+++ b/modules/entity_form/entity_browser_entity_form.module
@@ -48,8 +73,8 @@ function entity_browser_entity_form_inline_entity_form_reference_form_alter(&$re
   $bundles = $reference_form['entity_id']['#selection_settings']['target_bundles'];

Not sure if this belongs in this issue, but this line makes an assumption there are target bundles, which may not be the case so we get a warning here.

s_leu’s picture

As mentioned in #12, the patch here combined with the one of the MR in #3253939: Add the parent entity to the widget state to fix base fields for Entity Browser fix a problem I encounter when using the an entity browser in a field with inline entity form complex formatter. Without the patch I get the error message mentioned in https://www.drupal.org/project/inline_entity_form/issues/3253939#comment-14478254

Also regarding #15, this seems to have been addressed in another issue: #3283482: Entity browser inline form widget throws warning for bundle-less entities

anybody’s picture

Version: 8.x-2.6 » 8.x-2.x-dev

Please turn this into a MR if still relevant and address the comments above.

berdir made their first commit to this issue’s fork.

berdir’s picture

Status: Needs work » Postponed

It still is relevant, but it's also still postponed on the IEF issue, it only works when combined with that and that's a hard blocker.

There was already an MR, I rebased it, conflicted only on coding style fixes.