By dries on
Change record status:
Draft (View all draft change records)
Project:
Introduced in branch:
main
Introduced in version:
12.1.0
Issue links:
Description:
A data definition describes a value's type and constraints, but until now it couldn't include examples. Examples help developers and AI agents understand what a valid value looks like.
You can now add examples with setExamples() and read them with getExamples():
$properties['color'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Color'))
->setExamples(['blue', 'red']);
$properties['color']->getExamples();
// Returns ['blue', 'red'].
Examples are documentation only: Drupal doesn't validate them or use them as values. Examples can be used by Tool API, OpenAPI, and other contributed modules. Core doesn't output them yet; but that work is planned in #3623432: Emit data definition example values as the JSON Schema "examples" keyword.
If your class implements DataDefinitionInterface without extending DataDefinition, add:
public function getExamples(): array {
return [];
}
Impacts:
Module developers