schema_form
This module automatically generates Drupal Forms directly from defined schemas, using the structure, labels and validation constraints from the schema definition.
It auto generates forms for configuration entities, config objects like module settings page, and for any other schema you want!
Simply put, to create a form page, you should only describe the data schema in a YAML schema file, and that's it. The form page will be generated automatically without custom code at all. But, if you need, you still can alter the form and add your customizations automatically over auto-generated form.
Schema Form Designs
You can customize the form by adding and overriding the schema data using the Schema Form Design entities, without modifying the data schema itself.
Examples
Here are some short examples:
Configuration form (API settings form)
file config/schema/my_module.schema.yml:
my_module.my_api_settings:
type: config_object
label: My API Settings
mapping:
endpoint:
type: string
label: Endpoint URL
description: URL of the my-api.com endpoint.
access_token:
type: string
label: Access Token
description: An alphanumeric string 32 characters long.file my_module.routing.yml:
my_module.my_api_settings:
path: '/admin/config/system/my-api'
defaults:
_form: 'Drupal\schema_form\SchemaConfigFromRouteForm'
_title: 'My API Settings'
requirements:
_permission: 'administer site configuration'
options:
editable_config_names:
- my_module.my_api_settingsAnd that's all that you need! Only two YAML files generate a fully working configuration page with auto-generated form, validation and saving the values to the Drupal config.
Auto-generated basic Site Settings form (from the Drupal Core) with no code
The system.site schema is already defined in the Drupal Core, so we need only a single file to declare a route, connected to this schema:
file my_module.routing.yml:
my_module.basic_site_settings:
path: '/admin/config/system/site-information-nocode'
defaults:
_form: 'Drupal\schema_form\SchemaConfigFromRouteForm'
_title: 'Basic site settings'
requirements:
_permission: 'administer site configuration'
options:
editable_config_names:
- system.siteAnd that's it! This route automatically generates a page /admin/config/system/site-information-nocode with the form, similar to the Drupal Core's form, written using 200+ lines of the custom PHP code.
See more detailed documentation and examples here.
Custom Config Entity forms
file config/schema/my_module.schema.yml:
my_module.my_config_entity.*:
type: config_entity
label: My config entity
mapping:
id:
type: string
label: ID
description: Unique identifier for the config entity.
myString:
type: string
label: My String
description: A string value.
myNumber:
type: integer
label: My Number
description: An integer value.See more detailed documentation and examples here.
Regular forms
You can use this module not only for configuration forms, but for any form you want! Instead of building a long PHP array for the form, you just need to describe the schema and that's it. See the examples in the separate documentation here.
Documentation
More detailed documentation and examples are published on a separate resource: Schema Form Drupal module documentation »
Similar projects
- Automatic Configuration Form - implements the same idea, but targeted only at configuration forms. Does not use plugins, so support is limited to only built-in field types.
- Schema Based Config Forms - targeted only at configuration forms, extendable by plugins and provides more field types out of the box.
Alternatives, based on content entities »
If you use those modules, you can simply replace them with this module Schema Form without any change in your created schemas, because it supports form declaration formats from these modules too.