Resources and operations¶
A resource is a thing with identity and storage: a config object, a placed block, a content type, a node. An operation is a named action on it, with its input, a permission and its checks. A form, a Tool API tool, an HTTP call and Drush are each one way to run an operation, and all of them run the same checks.
Entity types already have most of this: storage, access per field and, for content entities, validation that JSON:API and REST run on a write. A simple config object has none of it. Its form class holds the checks and its route holds the permission, so a Drush command, a config action or an agent that writes the same settings asks no permission and runs no validation unless its author wrote them. Operations give a config object named actions, each with a permission, and validates a write through one against its config schema before it saves.
Operations¶
A resource sits beside the forms a module already has. Each form is one operation, and the forms keep working at their own routes. Two shapes cover most of core.
A collection holds many things, each with an id, such as placed blocks. Its operations are the standard ones: create, update and delete, and a read of the current values.
| Core's form today | Operation | Tool API tool | HTTP (TBD) |
|---|---|---|---|
/admin/structure/block/add/{plugin_id}/{theme} |
create | surface:block.placement:create |
POST /surface-api/block.placement |
/admin/structure/block/manage/{block} |
update | surface:block.placement:update |
PATCH /surface-api/block.placement/{id} |
/admin/structure/block/manage/{block}/delete |
delete | surface:block.placement:delete |
DELETE /surface-api/block.placement/{id} |
| read | surface:block.placement:read |
GET /surface-api/block.placement/{id} |
A singleton is one thing with no id, such as a config object. It has a read and one update, named configure.
| Core's form today | Operation | Tool API tool | HTTP (TBD) |
|---|---|---|---|
/admin/config/system/site-information |
configure | surface:system.site:configure |
PATCH /surface-api/system.site |
| read | surface:system.site:read |
GET /surface-api/system.site |
The schema of an operation¶
Every operation says what it accepts before it is called, as a Portable Schema document. For a placed block:
| To find out | Tool API | HTTP (TBD) |
|---|---|---|
| What create accepts | The input schema of surface:block.placement:create |
GET /surface-api/block.placement/schema |
| What update accepts for one block | The input schema of surface:block.placement:update |
GET /surface-api/block.placement/{id}/schema |
| What create accepts given some answers, such as the settings once a plugin is chosen | surface:block.placement:create:schema |
POST /surface-api/block.placement/schema, with the answers so far |
| What is stored now | surface:block.placement:read |
GET /surface-api/block.placement/{id}, with the schema beside the values |
Asking with answers writes nothing. The OpenAPI document holds the schema of every operation at once. A form and the Drush command read the same structure, so what they ask and accept does not differ.
From the command line¶
Any operation also runs from the command line. The proof of concept adds one Drush command that runs an operation as an account, through the same checks. Without --values it only says whether the account may run the operation and which keys it takes:
drush surface:run system.site configure --uid=1 --values='{"slogan":"Built with Drupal"}' --dry-run
Operations are declared on the resource's class with attributes, as plugins and hooks are. As an example, this is the start of the proof of concept's resource for placed blocks, which core does not have: the resource and its create operation (Example 2 shows the rest):
#[Resource('block.placement',
label: new TranslatableMarkup('Placed block'),
description: new TranslatableMarkup('A block placed in a region of a theme under a machine name: its label, the settings its plugin takes, its weight, its status and when it shows.'),
identity: ['id'],
storage: BlockPlacementResourceStorage::class,
)]
#[Operation('create',
label: new TranslatableMarkup('Place a block'),
description: new TranslatableMarkup('Places a block under a machine name the caller chooses, in a region of a theme, with the settings its plugin takes; a machine name already in use is refused.'),
permission: self::PERMISSION,
creates: TRUE,
idempotent: TRUE,
)]
final class BlockPlacementResource implements ResourceInterface {
And a singleton's, core's site information settings, as surface_examples_site declares them (its keys):
#[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',
)]
The read is derived from the update.
Where an operation's keys come from¶
- Declared by the module that owns the config. Each key is declared in Portable Schema with its title, description, default and checks (Structures). Core's common settings forms would be converted ahead of time. A declared key is checked against the config schema at its path, so it can only narrow what config schema allows.
- Read where the owner has not converted. A resource over another module's config names the keys it takes, and each key's type, label and constraints are read from that module's config schema. Declaring them would copy rules the resource does not own. This is the bridge until the owner converts, and config schema is not changed.
HTTP and OpenAPI¶
Every operation is also an HTTP endpoint: the path names the resource and the method names the operation. One OpenAPI 3.1 document, at /surface-api/openapi.json in the proof of concept, describes every endpoint with the schema of what each operation accepts.
TBD
HTTP and OpenAPI are assumed throughout this site. Where they live, in core or in a module, and how they relate to JSON:API and REST, is open.
Access¶
An operation names a permission that already exists, usually the one the form's route asks today. None are created. Beyond the permission, an access class may answer per key: open, locked (shown but fixed), or left out, as field access does per field. A resource over another module's things asks that module's access each time, so node grants and workflow modules still apply.
Validation¶
A write through an operation validates the whole config object, the new values over the stored ones, against its config schema before anything is saved. Only the new way in is strict: Config::save(), a config action and a hand-written form behave as they do today.
Dry runs¶
Any operation that writes can be run without writing. A dry run asks the permission, checks the input, and prepares the write as storage would, with storage's own checks, then stops. It answers whether the call would be accepted, which stored values would change, and what would be refused and why. An agent can check a call before making it, and a person can see what one would do.
| Way in | How |
|---|---|
| Tool API tool | dry_run as an input |
| HTTP (TBD) | ?dryRun=true on the same request |
| Drush | --dry-run |
Checking is not previewing. A dry run says what would be stored, not how a page would look, which belongs to what renders it. Keeping several writes together to look at before they are applied is staging, which stays in the proof of concept for now.
Storage¶
Storage stays where it is: a config object, a config entity saved through its own save(), a plugin's configuration, a content entity. Adopting needs no update path and leaves a config export as it was.
Forms¶
Generated forms are optional. A hand-written form can keep running the operation, and a module replaces it with a generated one at its own pace, or never. Automatic form generation will scale from small simple forms to complex, dynamic use cases, and form alters stay available for cosmetic changes. Because a form is drawn from the Portable Schema document, a headless client or a React app can draw the same form.
Widgets¶
Field widgets are not changed. A generated form chooses its own elements from each key's schema. Whether the two grow toward one shared contract is a question for another day.
Operations are part of the foundation of the path into core, and generated forms come last for each kind of data.