Structures¶
A structure describes a thing's data: each key, its type, its allowed values and its rules, with what a caller needs to send a valid value. A form, a Tool API tool, an HTTP endpoint and Drush all read the same structure, and so does validation, so they accept and refuse the same values.
How a structure is made¶
A module declares in Portable Schema the keys it owns. Until it does, a structure is read from what already describes the data: config schema, field definitions, or a JSON Schema document such as a component's props. A module changes nothing to be read. But a structure read this way can only say what its source can say, and the groundwork adds to what core's definitions can: defaults, labels for choices, and rules that depend on another key.
Declared in Portable Schema¶
This is how a module converts its own settings, and how core's common settings forms would be converted ahead of time. Each key carries its title, description, default and checks, written with a builder that produces JSON Schema.
As an example, these are four of the keys of core's site information settings, as surface_examples_site declares them. It is an example module in the proof of concept that plays core converting its own settings:
$inputs->add(self::NAME, Schema::string()->minLength(1)
->title(t('Site name'))
->check('drupal:Regex', self::singleLine())
->required());
$inputs->describe(self::NAME, group: $details);
$inputs->add(self::SLOGAN, Schema::string()
->title(t('Slogan'))
->description(t("How this is used depends on your site's theme."))
->maxLength(255)
->check('drupal:Regex', self::singleLine()));
$inputs->describe(self::SLOGAN, group: $details);
$inputs->add(self::MAIL, Schema::string()
->title(t('Email address'))
->description(t("The <em>From</em> address in automated emails sent during registration and new password requests, and other notifications. (Use an address ending in your site's domain to help prevent this email being flagged as spam.)"))
->format('email')
->required());
$inputs->describe(self::MAIL, group: $details);
$inputs->add(self::PAGE, Schema::object([
self::FRONT => Schema::string()
->title(t('Default front page'))
->description(t('Specify a relative URL to display as the front page.'))
->default('/user/login')
->pattern('^/')
->check('drupal:ValidPath')
->required(),
self::PAGE_403 => Schema::string()
->title(t('Default 403 (access denied) page'))
->description(t('This page is displayed when the requested document is denied to the current user. Leave blank to display a generic "access denied" page.'))
->pattern('^/')
->check('drupal:ValidPath'),
self::PAGE_404 => Schema::string()
->title(t('Default 404 (not found) page'))
->description(t('This page is displayed when no other content matches the requested document. Leave blank to display a generic "page not found" page.'))
->pattern('^/')
->check('drupal:ValidPath'),
])->title(t('Pages'))->required());
$inputs->describe(self::PAGE . '.' . self::FRONT, group: $front);
$inputs->describe(self::PAGE . '.' . self::PAGE_403, group: $errors);
$inputs->describe(self::PAGE . '.' . self::PAGE_404, group: $errors);
A declared key is checked against the config schema at its path, so it can only narrow what config schema allows, and config schema keeps being read and written as today. Portable Schema is meant to eventually succeed Typed Data and config schema as the way data is described. That is a long way off.
Read from config schema¶
This is the bridge for config another module owns and has not converted, and for plugins and config with no resource class. A mapping becomes a group of keys, a sequence becomes a list, and a type chosen by a sibling key ([%parent.plugin]) becomes keys that depend on that sibling. The module writes nothing and changes nothing: a block plugin that has only config schema can already be placed this way.
Read from field definitions¶
An entity type already has identity, storage, operations and access, so each bundle's resource is derived from its field definitions, one structure per field. A field's structure is always read, because fields are set up on each site.
Read from JSON Schema¶
Some data already has a JSON Schema: a single-directory component's props, a Tool API tool's input, an OpenAPI component. The proof of concept reads such a document into a structure, so it is drawn, validated and served like any other. enum becomes choices, labeled from the component's meta:enum where it has one, and default, minLength or pattern become the key's own.
A component's props are the main case. Each component on a site declares its own, in its *.component.yml file. A block that lets a site builder choose a component reads the props of whichever is chosen, and the component's author writes nothing more (Example 2).
A rule a key cannot hold is kept and checked on the server, and a part that cannot be read at all is refused by name. Annotations that check nothing, such as deprecated, are left out, and so is a prop typed as a PHP class.
TBD
Reading a JSON Schema document is one of the proof of concept's experimental parts. How far core would take it is open (Questions for another day).
What a structure can say¶
Keys that depend on another key¶
A rule can depend on another key's value. "Without a license, at most 100" is a condition from a closed list and an effect that only narrows, written as JSON Schema if/then. A list can narrow by a sibling, such as a store's regions by its country, and a group of keys can be chosen by a sibling, as a block's settings are chosen by its plugin id.
Choices¶
A list of allowed values carries its labels, so the list that validates is the list the form offers and the schema states. Where the site decides the list, such as languages or content types, a class lists it for the site. These lists answer core's OptionsProviderInterface, so code that already reads that interface works unchanged.
Secrets¶
A key whose stored value is never read back, such as an API key, is marked with JSON Schema's writeOnly. This key is from the settings of an example webhook, in surface_examples:
$inputs->add('api_key', Schema::string()
->title(t('API key'))
->description(t('The key each post is signed with, pasted here.'))
->writeOnly(TRUE));
The form draws a password field that keeps the stored value when left empty, the Tool API tool marks the key write only, and no read returns it.
Computed values¶
A key whose value follows from other keys, such as a machine name from a label, is computed. This is the machine name of a content type, as an example module in the proof of concept declares it (surface_examples_node_type supplies a resource for content types, since core has none):
$inputs->compute(
'type',
Schema::of('drupal:config/machine_name')
->title(t('Machine name'))
->description(t('Unique machine readable name: lowercase letters, numbers, and underscores only.'))
->maxLength(EntityTypeInterface::BUNDLE_MAX_LENGTH),
from: ['name'],
using: MachineName::class,
when: Compute::UNLESS_SENT,
);
using names the class that computes it, from the values of the keys in from and nothing else. when says when: once, on create; again whenever a source changes; or, as here, only when the caller does not send one. The last is the exception: every other computed key is marked readOnly, a form draws it fixed, and a caller cannot set it.
It is checked by its own rules like any key, and a refusal names the keys it came from, since those are what the caller can change. A value that needs a service or the clock, such as a changed time, is not a computed key. Storage adds it.
Alter/Extend structures¶
A module adds its own keys to another module's structure in an alter class. It may narrow the owner's rules, never widen them. Its keys are stored under third_party_settings.<module>, so the owner's storage and schema never need to know them. Config entities already have this place, and simple config would get it (the path into core).
As an example, surface_examples has settings for an event registration, with a title and a capacity. A second module, surface_examples_compliance, stands in for a contributed one that extends it: it adds an event license, limits the capacity to 100 without one, and rewords the title's label. Its alter names the registration's class, RegistrationStep3Resource:
#[Alters(RegistrationStep3Resource::class)]
final class RegistrationComplianceAlter implements AltersInputsInterface {
// The translation trait, the State key and the constructor are left out.
/**
* {@inheritdoc}
*/
public function alterInputs(BlueprintAdditionsInterface $inputs): void {
// The structure's capacity is example 3's.
$inputs->attach('third_party_settings.surface_examples_compliance', ComplianceStructure::class, reads: ['capacity' => 'capacity']);
$inputs->describe('title', label: $this->t('Public event title'));
$inputs->describe('third_party_settings.surface_examples_compliance', label: $this->t('Compliance'), after: 'capacity');
// Without a license, no more than a hundred, whatever the room seats.
// The license is the structure's, which this alter reads at its mount.
// A license its own pattern refuses is no license: a rule never sees
// an invalid sibling.
$inputs->narrow('capacity')
->when(Condition::absent('license'))
->constrain(Schema::any()->maximum(100))
->because($this->t('Up to 100 without an event license.'));
}
}
The registration's class is unchanged and does not know the second module exists. Today a form alter changes one form and nothing else that takes the same values, and hook_config_schema_info_alter() can loosen any module's rule with no record of who did. Here one alter reaches every way in, and every narrowing is checked. What contrib sees compares the two.
Structures are part of the foundation of the path into core.