Example 1: Site information¶
Site information is a basic settings form: one config object, edited by one form behind one permission. Simple config like this has a schema and nothing else: no named actions, no access of its own, no validation on write. Rung 1 gives it a resource beside its existing form. This example plays core converting its own settings, so the resource declares every key in Portable Schema. The form keeps working as it is.
Today¶
Core's site information form edits six keys of system.site: the site name, the slogan, the email address, the front page and the two error pages. Its route asks "administer site configuration", and only that route asks it. A Drush command, a config action, a recipe or a Tool API tool that writes system.site asks no permission and runs no checks unless its author wrote them.
The form's rules live in its PHP: titles and descriptions, which fields are required, the slogan's length, and in validateForm() the check that each page is a path of this site. Config schema types the three pages as plain strings and marks nothing required.
The change¶
In core, two classes would sit beside the files the system module has today:
File in core/modules/system |
What it holds | With the change |
|---|---|---|
src/Form/SiteInformationForm.php |
The form: labels, required fields, checks | Unchanged |
system.routing.yml |
The form's route and its permission | Unchanged until rung 3 |
config/schema/system.schema.yml |
The keys' types | Unchanged |
config/install/system.site.yml |
The install values | Unchanged |
src/Resource/SiteInformationResource.php |
The keys, the operations and their permission | Added |
src/Storage/SiteInformationStorage.php |
The config object the keys are stored in | Added |
In the proof of concept the two classes live in the example module surface_examples_site.
TBD
The directory and class names are the proof of concept's; core's are open.
The resource names the config object, the operation and its permission, which already exists:
#[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',
)]
Each key is declared once, with what core's form gives it:
$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);
The storage class names the config object. The keys are declared as core stores them, the three pages under page, so no key is renamed between the request and the config file:
final class SiteInformationStorage extends ConfigObjectResourceStorage {
/**
* {@inheritdoc}
*/
public const CONFIG = 'system.site';
}
Each declared key is held to core's config schema at its path when the resource is built, so it can only narrow what the schema allows. Core's label type refuses control characters, so the name and the slogan state that check too. The check that each page is a path of this site the account can open is core's own ValidPath constraint, the one path aliases use.
What is added:
| File | Code lines |
|---|---|
| The resource | 81 |
| The storage | 7 |
| Total | 88 |
| Optional, at rung 3: the route points at the generated form (below) | 1 |
Removed: nothing
Core's form, its route and its config schema are not touched. Code lines leave out blank lines and comments.
What it gets¶
Two operations. The resource declares configure, and a read is added beside it that returns the stored values and asks the same permission, so an account that may not change the settings may not read them either. Each runs the same way at every way in:
| What | Where |
|---|---|
| Tool API tool | surface:system.site:configure and surface:system.site:read |
| Drush | drush surface:run system.site configure |
| HTTP | PATCH /surface-api/system.site and GET /surface-api/system.site (TBD) |
| OpenAPI | Both endpoints, with the schema of what configure accepts, in /surface-api/openapi.json (TBD) |
| Generated form | /admin/config/system/site-information, once its route points at the generated form (below) |
TBD
HTTP and OpenAPI are assumed. Where they live, in core or in a module, is open, and the paths shown are the proof of concept's.
A write through any of them asks "administer site configuration", runs the same checks, and keeps the keys the resource does not declare as they were. An address that is not an email address is refused at /mail, and nothing is written.
Until its route is swapped, core's form at /admin/config/system/site-information saves through Config::save() as it does today, and does not run the operation's checks.
Swapping in the generated form¶
Rung 3 points the form's route at the operation. The form is then built from the resource on each request: the declared titles, descriptions, defaults and groups, saved through the operation's checks. On core's own route, one line changes:
system.site_information_settings:
path: '/admin/config/system/site-information'
defaults:
- _form: '\Drupal\system\Form\SiteInformationForm'
+ _surface_form: 'system.site:configure'
_title: 'Basic site settings'
requirements:
_permission: 'administer site configuration'
The proof of concept puts the same default on a second route, /admin/config/system/surface-site-information, so core's form stays as it is. With the route swapped, SiteInformationForm can be deleted at rung 4.
TBD
The route key's name is the proof of concept's; core's is open.
Before the owner converts¶
Core would convert its own settings as above. Until it does, another module can still put a resource over a core config object, for example to offer it as Tool API tools. That module reads the keys from core's config schema, so it gets core's types, labels and checks without copying them. It is a bridge: once core declares the keys, the module uses core's resource instead.
The performance example is such a module, over system.performance. It reads the two aggregation keys. Config schema allows any integer for the page cache's maximum age, so it declares that one key with the periods core's form offers. A declared key is held to config schema at its path, so it can only narrow what core allows:
$inputs->add(self::MAX_AGE, Schema::options($periods)
->type('integer')
->title(t('Browser and proxy cache maximum age'))
->description(t('This is used as the value for max-age in Cache-Control headers.'))
->default(0)
->required());
$inputs->describe(self::MAX_AGE, group: ArrangementGroup::details('caching', t('Caching')));
// Read from core's schema: its labels, no default, and not required,
// since system.performance is not fully validatable.
$inputs->fromConfigSchema(self::CSS_PREPROCESS, self::JS_PREPROCESS);
Try it¶
drush pm:install surface_examples_site
A dry run through the Tool API tool:
drush tool:run surface:system.site:configure --uid=1 --input='{"values":{"page":{"front":"/user/login"}},"dry_run":true}'