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.

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.
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | conditional-fields-reset.gif | 1.83 MB | joelpittet |
| checkbox-screenshot.png | 39.33 KB | odensc |
Issue fork conditional_fields-3573010
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
Comment #2
odenscComment #4
odenscI'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.Comment #5
odenscphpunit 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.
Comment #6
jyotimishra-developer commentedIts not working
Comment #7
odenscCan you provide steps to reproduce the issue?
Comment #8
jyotimishra-developer commentedfor me after applying patch, its not reseting the value of hidden fields.
Comment #9
jyotimishra-developer commented@odensc Could you please specify the steps how did you test ? May b I am missing something here ?
Comment #10
kenwest commentedI'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) {
Comment #11
joelpittetRe-targetting to the 4.x dev branch because that is where we will fix this.
Comment #12
joelpittetRerolled the MR the only conflict to resolve was this line
$error_key = implode('][', $error_key_arr ?? []);to
$error_key = implode('][', $error_key_arr);Comment #13
joelpittetOne blocker remains: @kenwest's #10 paragraph-subform fatal is still unfixed.
@odensc: in
formValidate(), the subform branch derives$field_namefrom#parentsand later calls:$entity->getFieldDefinition($field_name)->getDefaultValue($entity);For paragraph subforms,
#parentscan end invalue, while$entityis the host node. That makesgetFieldDefinition('value')return NULL and fatals.There is already working subform logic around line 600 that finds the last
subformwitharray_reverse(..., TRUE)and derives the actual field name fromarray_parents. Please reuse that approach. The current plainarray_search()finds the firstsubform, 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
subformhandling?Comment #14
joelpittet@kenwest Can you have a look at dev branch now that #3501575: TypeError: NestedArray::getValue(): Argument #2 ($parents) must be of type array, null given in ConditionalFieldsFormHelper::formFieldGetValues() is in there as it deals with subforms
Comment #15
joelpittetMy manual testing scenario:
Set up the reported scenario
At
admin/structure/types/manage/article/fields, add two plain-text fields:field_a, andfield_bwith default valueDefault. Then atadmin/structure/types/manage/article/conditionalsadd a dependency — dependentfield_b, dependeefield_a, state Visible, condition Value, "widget" values mode, valuetrigger— and tick "Reset the target to its default values when the form is submitted if the dependency is not triggered."Comment #16
joelpittetI 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.
Comment #17
kenwest commentedHi @joelpittet I was retesting as you requested and just noticed this too
Comment #18
kenwest commentedI'll test 4e013b11.
Comment #19
joelpittetThanks 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
Comment #20
kenwest commentedLooks 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.