Two fields depending on each other: Call to undefined method Drupal\conditional_fields\Form\ConditionalFieldEditForm::getEntity()

Created on 9 August 2024, 8 months ago

Problem/Motivation

I have two fields (Name and Email) that are optional, but if either is filled out, they both should be. Thus I have two conditions:

  1. If Name is filled, Email is required.
  2. If Email is filled, Name is required.

This works in 4.0.0-alpha5, but with 4.x-dev (commit ccacfa3), the following error is thrown when you try to save the second condition (regardless of which is created first).

Interestingly, the conditions do seem to work. You just can't save any changes to the conditions' settings.

Error message

Error: Call to undefined method Drupal\conditional_fields\Form\ConditionalFieldEditForm::getEntity() in Drupal\conditional_fields\ConditionalFieldsFormHelper::dependentValidate() (line 440 of modules/contrib/conditional_fields/src/ConditionalFieldsFormHelper.php).

call_user_func_array(Array, Array) (Line: 282)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 238)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 238)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'conditional_field_edit_form') (Line: 118)
Drupal\Core\Form\FormValidator->validateForm('conditional_field_edit_form', Array, Object) (Line: 593)
Drupal\Core\Form\FormBuilder->processForm('conditional_field_edit_form', Array, Object) (Line: 325)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 50)
Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇺🇸United States jvogt Seattle, WA

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

Comments & Activities

  • Issue created by @jvogt
  • 🇺🇸United States jvogt Seattle, WA

    I think it's due to this line being remove in commit 7ec4b704 (line 399 in src/ConditionalFieldsFormHelper.php), causing the dependentValidation to execute when saving conditions:

     if (!isset($form['#conditional_fields'])) {
      return;
    }
    

    If you add that back in just after the following, the error goes away and the conditional field rules work:

    $inline_entity_form_parents = self::findInlineEntityFormParentsForElement($form, $element);
    if ($inline_entity_form_parents) {
      $form = NestedArray::getValue($form, $inline_entity_form_parents['element_parents']);
    }
    

    I don't know if this is the correct solution as the removal may have been intentional in issue 2856720 Support for Inline Entity Form Fixed . However, I tested the fix with my initial conditions and the conditional fields inside an "Inline entity form - Simple" and it worked. I didn't test with "Inline entity form - Complex".

  • 🇸🇰Slovakia coaston

    +1 I have got the same issue.
    I am not using Inline Entity Form and I do not have installed it.

    In my case I have used dependency to field "B" and then I reused this field in another content type . However it takes from some reason dependency and it is displayed in the second content type and also to field "B" however such field "B" is not reused in the second content type, just the field "A" has been reused.
    When I want to update that dependency now in the first content type I am getting this issue

    But this

    if (!isset($form['#conditional_fields'])) {
    return;
    }

    Resolved the issue.

    Please try to create a patch.

  • 🇺🇸United States jvogt Seattle, WA

    Thanks for confirming! Here's a patch.

  • Status changed to Needs review 8 months ago
  • 🇫🇷France dqd London | N.Y.C | Paris | Hamburg | Berlin
  • Status changed to Needs work about 2 months ago
  • 🇬🇧United Kingdom malcomio

    I had observed the same error when trying to add a second dependency:

    1. Field 2 is required if field 1 has value X
    2. Field 3 is visible if field 2 has value Y

    This is not quite the same scenario as the original description, but very similar.

    After applying the patch, I got the following error:

    TypeError: Drupal\conditional_fields\ConditionalFieldsFormHelper::addStateToGroup(): Argument #1 ($new_states) must be of type array, null given, called in /var/www/html/docroot/modules/contrib/conditional_fields/src/ConditionalFieldsFormHelper.php on line 276 in Drupal\conditional_fields\ConditionalFieldsFormHelper->addStateToGroup() (line 478 of modules/contrib/conditional_fields/src/ConditionalFieldsFormHelper.php).
    Drupal\conditional_fields\ConditionalFieldsFormHelper->processDependeeFields(Array, Array, Array, Array, '') (Line: 154)
    Drupal\conditional_fields\ConditionalFieldsFormHelper->processDependentFields() (Line: 108)
    Drupal\conditional_fields\ConditionalFieldsFormHelper->afterBuild(Array, Object) (Line: 184)
    conditional_fields_form_after_build(Array, Object)
    call_user_func_array('conditional_fields_form_after_build', Array) (Line: 1082)
    Drupal\Core\Form\FormBuilder->doBuildForm('node_document_edit_form', Array, Object) (Line: 579)
    Drupal\Core\Form\FormBuilder->processForm('node_document_edit_form', Array, Object) (Line: 326)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 591)
    Drupal\conditional_fields\Form\ConditionalFieldEditForm->getDummyField('node', 'document', Array, Object, Array) (Line: 153)
    Drupal\conditional_fields\Form\ConditionalFieldEditForm->buildForm(Array, Object, 'node', 'document', 'field_lawful_basis', 'ade41c73-6578-4e92-ad0a-514a0cbc7d6d')
    call_user_func_array(Array, Array) (Line: 536)
    Drupal\Core\Form\FormBuilder->retrieveForm('conditional_field_edit_form', Object) (Line: 284)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 638)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 121)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 53)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
    Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 116)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 90)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 741)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
    
  • 🇮🇹Italy antonio.lepore Macerata

    Patch rerolled

Production build 0.71.5 2024