Drupal 8 introduces the concept of configuration entities (or configurables) that leverage the entity system's CRUD and pluggable storage functionality. The main difference between configuration entities and Drupal 7-style content entities is that configuration entities use the configuration system for storage, rather than the database. They differ from plain configuration in that they are typically user-created, and often need to fire and respond to hooks. Examples of potential configuration entities include:
- Content types
- Views
- Taxonomy vocabularies
- Contact categories
- Image styles
- etc.
To define a new configuration entity type, extend the ConfigEntityBase class and define an interface that extends ConfigEntityInterface.
In my_module/lib/Drupal/my_module/MyConfigurableInterface.php:
<?php
/**
* @file
* Contains \Drupal\my_module\MyConfigurableInterface.
*/
namespace Drupal\my_module;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface defining my configurable data object.
*/
interface MyConfigurableInterface extends ConfigEntityInterface {
// Any public methods your class has should be here as well.
}
?>In my_module/lib/Drupal/my_module/Plugin/Core/Entity/MyConfigurable.php:
<?php
/**
* @file
* Contains \Drupal\my_module\Plugin\Core\Entity\MyConfigurable.
*/
namespace Drupal\my_module\Plugin\Core\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\Annotation\EntityType;
use Drupal\Core\Annotation\Translation;
use Drupal\my_module\MyConfigurableInterface;
/**
* Defines a MyConfigurable configuration entity.
*
* @EntityType(
* id = "my_configurable",
* label = @Translation("My configurable data object"),
* controllers = {
* "storage" = "Drupal\Core\Config\Entity\ConfigStorageController"
* },
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* }
* )
*/
class MyConfigurable extends ConfigEntityBase implements MyConfigurableInterface {
/**
* The machine-readable ID for the configurable.
*/
public $id;
/**
* The human-readable label for the configurable.
*/
public $label;
/**
* The universal unique identifier for the configurable.
*/
public $uuid;
// Override methods from ConfigEntityBase as needed.
}
?>For guidelines on converting existing user-created configuration data to configuration entities, see How to upgrade data to configuration entities.