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

Created on 9 August 2024, 3 months ago
Updated 24 August 2024, 3 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

Needs review

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 3 months ago
  • πŸ‡«πŸ‡·France dqd London | N.Y.C | Paris | Hamburg | Berlin
Production build 0.71.5 2024