Describes how to define and manipulate content and configuration entities.
Entities, in Drupal, are objects that are used for persistent storage of content and configuration information. See the Information types topic for an overview of the different types of information, and the Configuration API topic for more about the configuration API.
Each entity is an instance of a particular "entity type". Some content entity types have sub-types, which are known as "bundles", while for other entity types, there is only a single bundle. For example, the Node content entity type, which is used for the main content pages in Drupal, has bundles that are known as "content types", while the User content type, which is used for user accounts, has only one bundle.
The sections below have more information about entities and the Entity API; for more detailed information, see https://www.drupal.org/developing/api/entity.
Entity types are defined by modules, using Drupal's Plugin API (see the Plugin API topic for more information about plugins in general). Here are the steps to follow to define a new entity type:
Entity routes, like other routes, are defined in *.routing.yml files; see the Routing API topic for more information. Entities may alternatively use an auto route provider class; there is an example of this at the end of this section. If providing routes directly, here is a typical entry, for the block configure form:
entity.block.edit_form: path: '/admin/structure/block/manage/{block}' defaults: _entity_form: 'block.default' _title: 'Configure block' requirements: _entity_access: 'block.update'
Some notes:
handlers = { "form" = { "default" = "Drupal\block\BlockForm",
handlers = { "route_provider" = { "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
For entity types that use bundles, such as Node (bundles are content types) and Taxonomy (bundles are vocabularies), modules and install profiles can define bundles by supplying default configuration in their config/install directories. (See the Configuration API topic for general information about configuration.)
There are several good examples of this in Drupal Core:
To load entities, use the entity storage manager, which is an object implementing \Drupal\Core\Entity\EntityStorageInterface that you can retrieve with:
$storage = \Drupal::entityManager()->getStorage('your_entity_type'); // Or if you have a $container variable: $storage = $container->get('entity.manager')->getStorage('your_entity_type');
Here, 'your_entity_type' is the machine name of your entity type ('id' annotation on the entity class), and note that you should use dependency injection to retrieve this object if possible. See the Services and Dependency Injection topic for more about how to properly retrieve services.
To query to find entities to load, use an entity query, which is a object implementing \Drupal\Core\Entity\Query\QueryInterface that you can retrieve with:
// Simple query: $query = \Drupal::entityQuery('your_entity_type'); // Or, if you have a $container variable: $query_service = $container->get('entity.query'); $query = $query_service->get('your_entity_type');
If you need aggregation, there is an aggregate query available, which implements \Drupal\Core\Entity\Query\QueryAggregateInterface:
$query \Drupal::entityQueryAggregate('your_entity_type'); // Or: $query = $query_service->getAggregate('your_entity_type');
Also, you should use dependency injection to get this object if possible; the service you need is entity.query, and its methods getQuery() or getAggregateQuery() will get the query object.
In either case, you can then add conditions to your query, using methods like condition(), exists(), etc. on $query; add sorting, pager, and range if needed, and execute the query to return a list of entity IDs that match the query.
Here is an example, using the core File entity:
$fids = Drupal::entityQuery('file') ->condition('status', FILE_STATUS_PERMANENT, '<>') ->condition('changed', REQUEST_TIME - $age, '<') ->range(0, 100) ->execute(); $files = $storage->loadMultiple($fids);
The normal way of viewing entities is by using a route, as described in the sections above. If for some reason you need to render an entity in code in a particular view mode, you can use an entity view builder, which is an object implementing \Drupal\Core\Entity\EntityViewBuilderInterface that you can retrieve with:
$view_builder = \Drupal::entityManager()->getViewBuilder('your_entity_type'); // Or if you have a $container variable: $view_builder = $container->get('entity.manager')->getViewBuilder('your_entity_type');
Then, to build and render the entity:
// You can omit the language ID, by default the current content language will // be used. If no translation is available for the current language, fallback // rules will be used. $build = $view_builder->view($entity, 'view_mode_name', $language->getId()); // $build is a render array. $rendered = drupal_render($build);
Entity types define their access permission scheme in their annotation. Access permissions can be quite complex, so you should not assume any particular permission scheme. Instead, once you have an entity object loaded, you can check for permission for a particular operation (such as 'view') at the entity or field level by calling:
$entity->access($operation); $entity->nameOfField->access($operation);
The interface related to access checking in entities and fields is \Drupal\Core\Access\AccessibleInterface.
The default entity access control handler invokes two hooks while checking access on a single entity: hook_entity_access() is invoked first, and then hook_ENTITY_TYPE_access() (where ENTITY_TYPE is the machine name of the entity type). If no module returns a TRUE or FALSE value from either of these hooks, then the entity's default access checking takes place. For create operations (creating a new entity), the hooks that are invoked are hook_entity_create_access() and hook_ENTITY_TYPE_create_access() instead.
The Node entity type has a complex system for determining access, which developers can interact with. This is described in the Node access topic.
Entity CRUD, editing, and view hooks
\Drupal\Core\Entity\EntityManagerInterface::getTranslationFromContext()
Name | Location | Description |
---|---|---|
hook_entity_access | core/lib/Drupal/Core/Entity/entity.api.php | Control entity operation access. |
hook_entity_create_access | core/lib/Drupal/Core/Entity/entity.api.php | Control entity create access. |
hook_ENTITY_TYPE_access | core/lib/Drupal/Core/Entity/entity.api.php | Control entity operation access for a specific entity type. |
hook_ENTITY_TYPE_create_access | core/lib/Drupal/Core/Entity/entity.api.php | Control entity create access for a specific entity type. |
Name | Location | Description |
---|---|---|
ConfigEntityBase | core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php | Defines a base configuration entity class. |
ConfigEntityListBuilder | core/lib/Drupal/Core/Config/Entity/ConfigEntityListBuilder.php | Defines the default class to build a listing of configuration entities. |
ConfigEntityStorage | core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php | Defines the storage class for configuration entities. |
ConfigEntityType | core/lib/Drupal/Core/Entity/Annotation/ConfigEntityType.php | Defines a config entity type annotation object. |
ContentEntityBase | core/lib/Drupal/Core/Entity/ContentEntityBase.php | Implements Entity Field API specific enhancements to the Entity class. |
ContentEntityType | core/lib/Drupal/Core/Entity/Annotation/ContentEntityType.php | Defines a content entity type annotation object. |
ContentTranslationHandler | core/modules/content_translation/src/ContentTranslationHandler.php | Base class for content translation handlers. |
EntityConfirmFormBase | core/lib/Drupal/Core/Entity/EntityConfirmFormBase.php | Provides a generic base class for an entity-based confirmation form. |
EntityDeleteForm | core/lib/Drupal/Core/Entity/EntityDeleteForm.php | Provides a generic base class for an entity deletion form. |
EntityForm | core/lib/Drupal/Core/Entity/EntityForm.php | Base class for entity forms. |
EntityHandlerBase Deprecated | core/lib/Drupal/Core/Entity/EntityHandlerBase.php | Provides a base class for entity handlers. |
EntityListBuilder | core/lib/Drupal/Core/Entity/EntityListBuilder.php | Defines a generic implementation to build a listing of entities. |
EntityType | core/lib/Drupal/Core/Entity/EntityType.php | Provides an implementation of an entity type and its metadata. |
EntityType | core/lib/Drupal/Core/Entity/Annotation/EntityType.php | Defines an Entity type annotation object. |
EntityViewBuilder | core/lib/Drupal/Core/Entity/EntityViewBuilder.php | Base class for entity view builders. |
RevisionableContentEntityBase | core/lib/Drupal/Core/Entity/RevisionableContentEntityBase.php | Provides a content entity with extended support for revisions. |
SqlContentEntityStorage | core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php | A content entity database storage implementation. |
Name | Location | Description |
---|---|---|
AccessibleInterface | core/lib/Drupal/Core/Access/AccessibleInterface.php | Interface for checking access. |
ConfigEntityInterface | core/lib/Drupal/Core/Config/Entity/ConfigEntityInterface.php | Defines a common interface for configuration entities. |
ContentEntityInterface | core/lib/Drupal/Core/Entity/ContentEntityInterface.php | Defines a common interface for all content entity objects. |
EntityHandlerInterface | core/lib/Drupal/Core/Entity/EntityHandlerInterface.php | Defines an interface for entity handlers. |
EntityInterface | core/lib/Drupal/Core/Entity/EntityInterface.php | Defines a common interface for all entity objects. |
EntityStorageInterface | core/lib/Drupal/Core/Entity/EntityStorageInterface.php | Defines the interface for entity storage classes. |
EntityViewBuilderInterface | core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php | Defines an interface for entity view builders. |
FieldableEntityInterface | core/lib/Drupal/Core/Entity/FieldableEntityInterface.php | Interface for entities having fields. |
Name | Location | Description |
---|---|---|
EntityDeleteFormTrait | core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php | Provides a trait for an entity deletion form. |
RevisionLogEntityTrait | core/lib/Drupal/Core/Entity/RevisionLogEntityTrait.php | Provides a trait for accessing revision logging and ownership information. |
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!entity.api.php/group/entity_api/8.1.x