Generated with the help of an LLM. Reproduced using an emulated AJAX request.
Problem/Motivation
When changing the formatter in a Layout Builder field block, checkbox settings appear unchecked even when their default value is TRUE. These incorrect values are saved unless the editor notices and corrects them.
The Formatter select in FieldBlock::blockForm() uses AJAX without rebuilding the form. Since the new formatter's checkboxes were not part of the submitted input, Form API treats them as unchecked rather than using their default values. See #1100170: Checkboxes default value is ignored by forms system during processing of AJAX request.
Field UI and Views avoid this problem by rebuilding the form when the formatter changes.
Core formatters affected include:
EntityReferenceLabelFormatter: Link label to the referenced entity (reproduced).IntegerFormatterandDecimalFormatter: Display prefix and suffix (same code path, not tested).
Steps to reproduce
- Enable Layout Builder for a content type and open Manage layout.
- Add an Authored by field block.
- With the Label formatter selected, confirm that Link label to the referenced entity is checked.
- Change the formatter to Entity ID, then back to Label.
Expected result: The checkbox is checked.
Actual result: The checkbox is unchecked.
Reproduced using an emulated AJAX request. The steps above still need browser verification.
Proposed resolution
Follow the approach used by Field UI (EntityDisplayFormBase::multistepSubmit()) and Views (EntityField).
Add a submit handler to the Formatter select that rebuilds the form. Use #limit_validation_errors to avoid validating unrelated block settings.
Remaining tasks
- Verify the steps to reproduce in a browser.
- Implement the form rebuild.
- Add a FunctionalJavascript regression test for checkbox defaults when changing formatters.
Issue fork drupal-3643483
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 #3
joelpittetAdded a regression test that switches between formatters and confirms the default checkbox value is preserved. The test fails without the fix.
The fix rebuilds the form when the formatter changes, clears the submitted formatter settings, and limits validation to the formatter type. Clearing the settings is necessary because Form API sets the new checkbox input to
NULLduring the AJAX request, so a rebuild alone leaves the checkboxes unchecked. See #1100170: Checkboxes default value is ignored by forms system during processing of AJAX request.One behaviour change worth noting: switching formatters now resets the settings to the new formatter's defaults rather than carrying over shared settings. This matches the behaviour in Views.
This doesn't affect #3643250: Layout builder's AJAX error when adding a Changed field block with time difference disabled: that warning happens when the form is submitted, and that request isn't rebuilt. So removed from issue summary.