8. Storing dynamic data in configuration
Apart from content entities there is a second type of entities in Drupal, the configuration entities. These have a machine-readable string ID and can be deployed between different environments along with the rest of the site configuration.
8.1. Create an entity class
Section titled “8.1. Create an entity class”While there are some distinctions, creating a configuration entity type is very similar to creating a content entity type.
Create a src/Entity/EventType.php with the following:
More information on the above
-
Configuration prefix:
To clearly identify the source of all configuration, the names of the respective configuration files of configuration entities are automatically prefixed with the module name (
eventin this case) and a period (.) as a separator. To distinguish different configuration entity types from the same module, each configuration entity type specifies a configuration prefix which is the second part of the configuration file name prefix followed by an additional period. The full name of a configuration entity’s configuration file is, thus,"$module_name.$config_prefix.$entity_id". -
Export properties:
Configuration entities do not have a notion of (base) field definitions like content entities. Instead simple PHP properties can be declared in the entity class to hold the values of the entity. The names of those properties need to be specified as export properties in the entity annotation.
8.2. Provide a configuration schema
Section titled “8.2. Provide a configuration schema”To ensure that the structure of each configuration object is correct, a schema is provided. When importing configuration from another environment, each configuration object is validated against this schema.
Add a config/schema/event.schema.yml with the following:
8.3. Install the entity type
Section titled “8.3. Install the entity type”-
Run
drush entity:updatesNote that there is no schema change
-
Create and save an event type
Run the following PHP code:
Note that there is a new row in the
{config}table with the nameevent.type.webinar -
Load the event type by its ID
Run the following PHP code:
Note that the proper label is returned.
-
Update the label of the event type
Run the following PHP code:
-
Delete the event type
Run the following PHP code:
Note that the row in the {config} table is gone.