Problem/Motivation

Typed data definitions describe a value’s type and constraints, but can’t specify a default. Other parts of Drupal handle defaults separately, so tools built from a data definition can’t easily use one.

Proposed resolution

Add an optional default to DataDefinition, with setDefault() and getDefault() methods. It’s descriptive metadata only; it won’t change how values are created or validated. The method names should avoid clashing with the existing field API.

Remaining tasks

User interface changes

None.

Introduced terminology

None.

API changes

Two new methods on DataDefinition (and potentially DataDefinitionInterface, subject to the BC discussion above). Purely additive; no existing method changes signature or behavior.

Data model changes

None. The definition array gains an optional key.

Release notes snippet

Data definitions can now declare a default value via DataDefinition::setDefault(). The default is descriptive metadata available to consumers such as JSON Schema emitters and form generators; it does not change how typed data is instantiated or validated.

AI usage (if applicable)

  • [x] AI Assisted Issue: This issue was generated with AI assistance, but was reviewed and refined by the creator.
  • [ x] AI Assisted Code: This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.

Issue fork drupal-3620920

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

michaellander created an issue. See original summary.

michaellander’s picture

Assigned: Unassigned » michaellander

michaellander’s picture

Issue summary: View changes
michaellander’s picture

Assigned: michaellander » Unassigned
Status: Active » Needs review
michaellander’s picture

Status: Needs review » Needs work

I called out the emitting of default values to json schema as a separate issue, but would it be more appropriate to include as part of this? Setting back to needs work to remove the `hasDefault()` method, as it's inconsistent with the rest of the class.

michaellander’s picture

Status: Needs work » Needs review

I created a separate issue for emitting to json schema #3623431: Emit data definition default values as the JSON Schema "default" keyword. Please let me know if the preference is to combine them.

michaellander’s picture

Issue summary: View changes
michaellander’s picture

Issue summary: View changes
mglaman’s picture

Issue tags: +rosetta