Problem/Motivation

After v4.0.0-alpha6, the checkbox option "Reset the target to its default values when the form is submitted if the dependency is not triggered." on a field condition does not work.

screenshot highlighting checkbox option

Expected behavior: When the dependency is untriggered, the dependent field value should be reset to the entity definition's default value for that field.
Current behavior: When the dependency is untriggered, the dependent field does not get reset.

Steps to reproduce

1:
Create a new content type or add to an existing content type:
- Text field A
- Text field B (set default value: "Default")

2:
Create a field condition on an content type:
- Target field (dependent): Text field B
- Controlled by (dependency): Text field A
- The target field is: Unchanged (no state)
- when the control field: is Filled

Click Add dependency[checked] Reset the target to its default values

3:
Create or edit an entity of that type. On the edit form, leave text field A empty and fill out text field B with a value such as "foobar".

4:
Save the entity. Since text field A is empty (not filled), the dependency was *not* triggered, so text field B (the dependent) should be reset, according to the checkbox.

5:
Observe that text field B wasn't reset to its default value ("Default").

Proposed resolution

It looks like #2902164: Controlled-by fields inside a Paragraph don't work inadvertently completely removed the code path to handle this checkbox option. For some reason it now just overwrites the form state value with the existing value: don't think this code even does anything currently.

Proposed resolution is restoring the code and adding a test to prevent this in the future. I will submit an MR.

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

odensc created an issue. See original summary.

odensc’s picture

Issue summary: View changes

odensc’s picture

I've submitted an MR that fixes this bug, and adds a test to prevent future regressions. It also refactors the validateForm logic to work within inline_entity_forms which was previously broken.

I think this should also fix #3495402, #3501575, #3548301, and #3548513 at the source. Those all have hacky patches to do null checks or ?? [] to work around the source issue, which is that the logic in validateForm was not correctly handling certain elements without parents.

odensc’s picture

phpunit for current major is failing, but that's due to an unrelated issue - should be fixed once #3573047: Get tests working again is merged. Previous major is succeeding.

jyotimishra-developer’s picture

Its not working

odensc’s picture

Can you provide steps to reproduce the issue?

jyotimishra-developer’s picture

for me after applying patch, its not reseting the value of hidden fields.

jyotimishra-developer’s picture

@odensc Could you please specify the steps how did you test ? May b I am missing something here ?

kenwest’s picture

I've tried the patch for MR93 on Drupal 11.3.10 with conditional_fields 4.0.0-alpha6. In my testing of this MR, I get an exception 'Error: Call to a member function getDefaultValue() on null' on 'line 848' (the MR says the line that throws that error should be 853 - perhaps another commit to the 4.x branch is impacting?)

My set up is a Node with a Paragraphs field and the paragraph has a Boolean field A and a Text field B. There is a dependency such that field B is visible if field A is checked. The exception is raised when field A is unchecked.

The line that raises the exception is : $default = $entity->getFieldDefinition($field_name)->getDefaultValue($entity);

When debugging, $entity is the Node and $field_name is 'value'. I expect $entity should be the Paragraph and $field_name should be 'field_subtitle' (that's the name of my field B, the target of the dependency).

This is my reading of the code, starting at the line: foreach ($untriggered_dependents as $field) {

  • The first few lines allow for the use of either Inline Entity Forms (IEF) or Subforms
  • For me, $inline_entity_form_parents is null and $subform_location is 3
  • The line that breaks $field_name is: $field_name = end($parents);
  • The last 3 entries in $parents are 'field_subtitle', 0 and 'value'. The code picks 'value' which is the attribute name not the field name
  • The line that breaks $entity is: if (isset($inline_entity_form_parents)) {
  • The previous line set $entity to the Node
  • But the 'if' only handles the IEF case - not the subform case - so $entity is not set to the Paragraph
joelpittet’s picture

Version: 4.0.0-alpha6 » 4.x-dev

Re-targetting to the 4.x dev branch because that is where we will fix this.

joelpittet’s picture

Status: Active » Needs review

Rerolled the MR the only conflict to resolve was this line
$error_key = implode('][', $error_key_arr ?? []);
to
$error_key = implode('][', $error_key_arr);

joelpittet’s picture

Status: Needs review » Needs work

One blocker remains: @kenwest's #10 paragraph-subform fatal is still unfixed.

@odensc: in formValidate(), the subform branch derives $field_name from #parents and later calls: $entity->getFieldDefinition($field_name)->getDefaultValue($entity);

For paragraph subforms, #parents can end in value, while $entity is the host node. That makes getFieldDefinition('value') return NULL and fatals.

There is already working subform logic around line 600 that finds the last subform with array_reverse(..., TRUE) and derives the actual field name from array_parents. Please reuse that approach. The current plain array_search() finds the first subform, which matters for nested paragraphs.

Two related guards are needed before dereferencing: confirm the form object provides an entity, and confirm getFieldDefinition() returned a field definition.

A good test to have to make sure we don't end with a regression is a paragraph-subform regression test for #10.

@kenwest: once updated, could you retest your nested-paragraph case, especially the first-vs-last subform handling?

joelpittet’s picture

Status: Needs work » Needs review
joelpittet’s picture

StatusFileSize
new1.83 MB

My manual testing scenario:

Set up the reported scenario

At admin/structure/types/manage/article/fields, add two plain-text fields: field_a, and field_b with default value Default. Then at admin/structure/types/manage/article/conditionals add a dependency — dependent field_b, dependee field_a, state Visible, condition Value, "widget" values mode, value trigger — and tick "Reset the target to its default values when the form is submitted if the dependency is not triggered."

manual test

joelpittet’s picture

I was able to reproduce the error @kenwest reported with paragraphs, so will need to fix that before this gets merged as I don't want to create a new bug to fix later. This branch is introducing the problem.

The website encountered an unexpected error. Try again later.
Error: Call to a member function getDefaultValue() on null in Drupal\conditional_fields\ConditionalFieldsFormHelper::formValidate() (line 911 of /var/www/html/src/ConditionalFieldsFormHelper.php).

kenwest’s picture

Hi @joelpittet I was retesting as you requested and just noticed this too

kenwest’s picture

I'll test 4e013b11.

joelpittet’s picture

Thanks for jumping on, it’s late for me so heading to bed, but we had paragraphs already in dev/tests so I thought might as well test this issue you brought up with it.

I am an IEF die-hard so I am planning getting tests in for that too. Something for tomorrow but locally paragraphs is not throwing errors, so I feel you might be in for a treat (I hope)

I’ll check back in the morning

kenwest’s picture

Looks like you've nailed it @joelpittet. The Exception is no longer thrown in the case I outlined above in comment #10. If the target field in the paragraph has a default, then it is set to the value of the default when the controlling field is unchecked. If it doesn't have a default, then it is emptied when the controlling field is unchecked. When the controlling field is checked, the value of the target field is untouched.