Generated with the help of an LLM. Problem experienced by me (a human type entity) in the UI

Problem/Motivation

Submitting the Add block form for a Changed field in Layout Builder triggers a PHP warning when using the Default timestamp formatter with Display as a time difference unchecked.

Warning: Undefined array key "enabled" in Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::settingsForm() (line 141)

When PHP warnings are displayed, the warning appears before the AJAX response JSON. The browser reports a parsererror even though the response has an HTTP status of 200.

Steps to reproduce

  1. Enable Layout Builder and configure the site to display PHP warnings.
  2. Go to Structure → Content types → Article → Manage display.
  3. Enable Use Layout Builder, save, and open Manage layout.
  4. Click Add block in a layout region and select the Changed field block.
  5. Keep the formatter set to Default and leave Display as a time difference unchecked.
  6. Click Add block and inspect the AJAX response in the browser's Network panel.

Expected result: The dialog submits successfully and the Changed field appears in the layout preview as a formatted date.

Actual result: The PHP warning makes the AJAX response invalid JSON when warning display is enabled.

The warning was reproduced on Drupal 11.4.14 by building Layout Builder's AddBlockForm with the raw input an unchecked checkbox produces. A fresh form and input with the checkbox checked both built without the warning.

The steps above still need browser verification on a clean site.

Proposed resolution

FieldBlock::getFormatter() can use raw form input before Drupal processes the checkbox. The browser submits the other time_diff settings but omits time_diff.enabled.

FormatterPluginManager::prepareConfiguration() fills in top-level defaults but not nested defaults. As a result, the partial time_diff array reaches TimestampFormatter::settingsForm() without the enabled key.

Have TimestampFormatter fill in missing nested time_diff defaults while preserving supplied values.

Remaining tasks

  • Verify the steps to reproduce on a clean Drupal installation.
  • Implement the fix.
  • Add regression coverage for submitting the Changed field block with the checkbox unchecked.
  • Review and commit.

Issue fork drupal-3643250

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

joelpittet created an issue. See original summary.

joelpittet’s picture

joelpittet’s picture

Status: Needs review » Active

joelpittet’s picture

Status: Active » Needs review

Ok, setting to needs review.

Added a regression test using the "Authored on" field, which uses the same formatter as Changed. Confirmed the test fails without the fix and passes with it. (tests-only run)

The fix in TimestampFormatter::getSetting() fills in the missing time_diff settings from the defaults.

There is potentially a wider issue here. FieldBlock::getFormatter() uses raw form input, and prepareConfiguration() doesn't account for missing nested settings. Other formatters in contrib could run into the same problem.

I'd prefer to keep this fix focused and address the more general problem in a follow-up.

joelpittet’s picture

Created #3643483: Layout Builder loses checkbox defaults when changing field formatters as a follow-up. This issue's fix covers the nested time_diff settings in TimestampFormatter. The follow-up covers a related bug in the same form: when the formatter type changes, FieldBlock doesn't rebuild the form, so checkboxes that default to checked show unchecked.

joelpittet’s picture

To clarify #5: #3643483: Layout Builder loses checkbox defaults when changing field formatters turned out to be a separate bug. The general problem, where getFormatter() passes incomplete nested settings from raw input, has no follow-up yet...

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Tested this one manually following the steps

1. Enabled layout builder and added to a content type
2. Went into layout for my content type and added the changed field
3. Verified in my logs I got Warning: Undefined array key "enabled" in Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter->settingsForm() (line 141 of /var/www/html/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php)
4. Applied the MR
5 Repeated again
6. Confirmed no log entries

Going to mark as this does fix the issue described but almost wonder PluginSettingsBase::getSetting() should be able to handle values like this? May be a follow up but just a thought.

joelpittet’s picture

Thanks 🙏 for the RTBC. It would be nice to have a generic solution. I’ll explore that in a follow-up for sure.

There was an example I was looking at (prior art) that I should dig up. This fix is very targeted to fix one problem but we will play whack-a-mole if we don’t find a general solution.