In my content type there are two fields: field_1(boolean) and field_2(file attachment).

Manage Dependies: field_2 is visible when field_1 has value "ON"

When try to create content and click on save, show this error:
"Undefined index: #entity_type in Drupal\conditional_fields\ConditionalFieldsFormHelper::dependentValidate() (line 331...modules/contrib/conditional_fields/src/ConditionalFieldsFormHelper.php)"

So, I have insert this in ConditionalFieldsFormHelper.php file on line 331:

 $entity_type = '';

            if (!empty($form['#entity_type'])) {
               $entity_type = $form['#entity_type'];
            }

Then replace this

$field = FieldStorageConfig::loadByName($form['#entity_type'], $dependent['field_parents'][0]);

With this:

$field = FieldStorageConfig::loadByName($entity_type, $dependent['field_parents'][0]);

and error is gone.

Comments

Kris77 created an issue. See original summary.

kris77’s picture

Issue summary: View changes
cweldon’s picture

StatusFileSize
new896 bytes

I followed the steps described in the issue and confirmed it resolved it for me. I created a patch based on Kris77's suggested resolution

boromino’s picture

Status: Active » Needs review
StatusFileSize
new1.41 KB
tgauges’s picture

I adjusted the patch so it applies to 4.0.0-alpha1.

ivnish’s picture

Status: Needs review » Reviewed & tested by the community

#5 works

ivnish’s picture

Version: 8.x-1.x-dev » 4.0.0-alpha1
Assigned: kris77 » Unassigned
colan’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/src/ConditionalFieldsFormHelper.php
@@ -372,6 +372,9 @@ class ConditionalFieldsFormHelper {
+    $entity_form = $form_state->getFormObject();
+    $entity_type_id = $entity_form->getEntity()->getEntityTypeId();

This looks fine, but (minor nit) can we combine these into a single line?

$entity_type_id = $form_state->getFormObject()->getEntity()->getEntityTypeId();

Shrinking the size of the method is more important than having less than 80 characters per line IMHO, and this would only be slightly over.

rassoni’s picture

Status: Needs work » Needs review
StatusFileSize
new1.05 KB
new905 bytes

Addressed #8feedback. Please review.

rishi.kulshreshtha’s picture

Version: 4.0.0-alpha1 » 4.x-dev
Status: Needs review » Reviewed & tested by the community

#9 looks good, and got applied cleanly to 4.x-dev as well. Marking this as RTBC.

  • heddn committed 7021c47c on 4.x authored by boromino
    Issue #3106135 by boromino, colan, heddn: Undefined index: #entity_type...
heddn’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for your contributions.

Status: Fixed » Closed (fixed)

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