Concepts¶
Three ideas carry the rest of this site: a structure, a resource with its operations, and Portable Schema. This page walks one example through all three. The example is core's own site information settings: the site name, slogan, email address, front page and error pages, edited at /admin/config/system/site-information. The proof of concept converts those settings in an example module, and the code here is from it.
A structure describes the data¶
A structure lists each key with its type, its label, its default and its rules. This is the key for the front page:
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(),
It must be a path, start with a slash and be one the account may visit, and it cannot be left empty. Core keeps the same rules today in the form's PHP, where only that form runs them.
A structure has no operations and is not stored by itself. A block's settings are an example. Until something uses the block, there is nothing to create, update or delete: block layout places it in a region, or a block field holds it. Whatever uses the block stores its settings and has the operations.
Structures says what else one can state, and how a structure is read from config schema where nobody has declared one.
A resource is a thing, and operations act on it¶
A resource is a structure with a place to store it, and the named actions that may be taken. Site information has one action, configure, and a read beside it. This declares the resource, the action and the permission it asks, which is the one the form's route asks now:
#[Resource('system.site',
label: new TranslatableMarkup('Site information'),
description: new TranslatableMarkup('The site name, slogan, email address, front page and error pages, as core keeps them in system.site.'),
storage: SiteInformationStorage::class,
)]
#[Operation('configure',
label: new TranslatableMarkup('Configure site information'),
description: new TranslatableMarkup('Sets the site name, slogan, email address, front page and error pages.'),
permission: 'administer site configuration',
)]
That operation is then reached in several ways, each derived from the declaration:
| Way in | For site information |
|---|---|
| Tool API tool | surface:system.site:configure |
| HTTP | PATCH /surface-api/system.site (TBD) |
| OpenAPI | The same endpoint, described |
| Drush | drush surface:run system.site configure |
| Form | A generated form, when wanted |
Whichever way it is called, the same steps run: ask the permission, check the input against the structure, then save. Resources and operations covers access, validation and storage.
Portable Schema is how it is written down¶
What an operation accepts is served as one document. It is JSON Schema with a few added keywords, called Portable Schema. This is the front page in the document for configure:
"page": {
"title": "Pages",
"type": "object",
"properties": {
"front": {
"title": "Default front page",
"description": "Specify a relative URL to display as the front page.",
"type": "string",
"pattern": "^/",
"default": "/user/login",
"portable-check": {
"drupal:ValidPath": {}
}
}
}
}
A client reads the label, the default and the pattern, and checks the value before it sends it. portable-check names a rule that only the server can check, here core's own ValidPath constraint, so the client knows one exists.
Where to go next¶
- Why. Why not Tool API tools, or forms from config schema.
- Adopting it. What a module does, one step at a time, starting with site information in full.
- The path into core. How this moves into core, and in what order.