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
- Follow-up issues: emit
defaultfrom the JSON Schema normalizers (See #3623431: Emit data definition default values as the JSON Schema "default" keyword); evaluate delegatingContextDefinition::getDefaultValue()to the underlying definition.
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
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
michaellander commentedComment #4
michaellander commentedComment #5
michaellander commentedComment #6
michaellander commentedI 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.
Comment #7
michaellander commentedI 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.
Comment #8
michaellander commentedComment #9
michaellander commentedComment #10
mglaman