W3cubDocs

/Drupal 8

Entity CRUD, editing, and view hooks

Hooks used in various entity operations.

Entity create, read, update, and delete (CRUD) operations are performed by entity storage classes; see the Entity API topic for more information. Most entities use or extend the default classes: \Drupal\Core\Entity\Sql\SqlContentEntityStorage for content entities, and \Drupal\Core\Config\Entity\ConfigEntityStorage for configuration entities. For these entities, there is a set of hooks that is invoked for each CRUD operation, which module developers can implement to affect these operations; these hooks are actually invoked from methods on \Drupal\Core\Entity\EntityStorageBase.

For content entities, viewing and rendering are handled by a view builder class; see the Entity API topic for more information. Most view builders extend or use the default class \Drupal\Core\Entity\EntityViewBuilder.

Entity editing (including adding new entities) is handled by entity form classes; see the Entity API topic for more information. Most entity editing forms extend base classes \Drupal\Core\Entity\EntityForm or \Drupal\Core\Entity\ContentEntityForm. Note that many other operations, such as confirming deletion of entities, also use entity form classes.

This topic lists all of the entity CRUD and view operations, and the hooks and other operations that are invoked (in order) for each operation. Some notes:

  • Whenever an entity hook is invoked, there is both a type-specific entity hook, and a generic entity hook. For instance, during a create operation on a node, first hook_node_create() and then hook_entity_create() would be invoked.
  • The entity-type-specific hooks are represented in the list below as hook_ENTITY_TYPE_... (hook_ENTITY_TYPE_create() in this example). To implement one of these hooks for an entity whose machine name is "foo", define a function called mymodule_foo_create(), for instance. Also note that the entity or array of entities that are passed into a specific-type hook are of the specific entity class, not the generic Entity class, so in your implementation, you can make the $entity argument something like $node and give it a specific type hint (which should normally be to the specific interface, such as \Drupal\Node\NodeInterface for nodes).
  • $storage in the code examples is assumed to be an entity storage class. See the Entity API topic for information on how to instantiate the correct storage class for an entity type.
  • $view_builder in the code examples is assumed to be an entity view builder class. See the Entity API topic for information on how to instantiate the correct view builder class for an entity type.
  • During many operations, static methods are called on the entity class, which implements \Drupal\Entity\EntityInterface.

Create operations

To create an entity:

$entity = $storage->create();

// Add code here to set properties on the entity.

// Until you call save(), the entity is just in memory.
$entity->save();

There is also a shortcut method on entity classes, which creates an entity with an array of provided property values: \Drupal\Core\Entity::create().

Hooks invoked during the create operation:

See Save operations below for the save portion of the operation.

Read/Load operations

To load (read) a single entity:

$entity = $storage->load($id);

To load multiple entities:

$entities = $storage->loadMultiple($ids);

Since load() calls loadMultiple(), these are really the same operation. Here is the order of hooks and other operations that take place during entity loading:

When an entity is loaded, normally the default entity revision is loaded. It is also possible to load a different revision, for entities that support revisions, with this code:

$entity = $storage->loadRevision($revision_id);

This involves the same hooks and operations as regular entity loading.

Save operations

To update an existing entity, you will need to load it, change properties, and then save; as described above, when creating a new entity, you will also need to save it. Here is the order of hooks and other events that happen during an entity save:

Some specific entity types invoke hooks during preSave() or postSave() operations. Examples:

Editing operations

When an entity's add/edit form is used to add or edit an entity, there are several hooks that are invoked:

Delete operations

To delete one or more entities, load them and then delete them:

$entities = $storage->loadMultiple($ids);
$storage->delete($entities);

During the delete operation, the following hooks and other events happen:

Some specific entity types invoke hooks during the delete process. Examples:

Individual revisions of an entity can also be deleted:

$storage->deleteRevision($revision_id);

This operation invokes the following operations and hooks:

View/render operations

To make a render array for a loaded entity:

// You can omit the language ID if the default language is being used.
$build = $view_builder->view($entity, 'view_mode_name', $language->getId());

You can also use the viewMultiple() method to view multiple entities.

Hooks invoked during the operation of building a render array:

View builders for some types override these hooks, notably:

During the rendering operation, the default entity viewer runs the following hooks and operations in the pre-render step:

Some specific builders have specific hooks:

After this point in rendering, the theme system takes over. See the Theme system and render API topic for more information.

Other entity hooks

Some types of entities invoke hooks for specific operations:

File

core/lib/Drupal/Core/Entity/entity.api.php, line 16
Hooks and documentation related to entities.

Functions

Name Location Description
hook_entity_build_defaults_alter core/lib/Drupal/Core/Entity/entity.api.php Alter entity renderable values before cache checking in drupal_render().
hook_entity_bundle_delete core/lib/Drupal/Core/Entity/entity.api.php Act on entity_bundle_delete().
hook_entity_create core/lib/Drupal/Core/Entity/entity.api.php Acts when creating a new entity.
hook_entity_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity deletion.
hook_entity_display_build_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the render array generated by an EntityDisplay for an entity.
hook_entity_field_values_init core/lib/Drupal/Core/Entity/entity.api.php Acts when initializing a fieldable entity object.
hook_entity_form_display_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the settings used for displaying an entity form.
hook_entity_insert core/lib/Drupal/Core/Entity/entity.api.php Respond to creation of a new entity.
hook_entity_load core/lib/Drupal/Core/Entity/entity.api.php Act on entities when loaded.
hook_entity_predelete core/lib/Drupal/Core/Entity/entity.api.php Act before entity deletion.
hook_entity_prepare_form core/lib/Drupal/Core/Entity/entity.api.php Acts on an entity object about to be shown on an entity form.
hook_entity_prepare_view core/lib/Drupal/Core/Entity/entity.api.php Act on entities as they are being prepared for view.
hook_entity_presave core/lib/Drupal/Core/Entity/entity.api.php Act on an entity before it is created or updated.
hook_entity_revision_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity revision deletion.
hook_entity_translation_create core/lib/Drupal/Core/Entity/entity.api.php Acts when creating a new entity translation.
hook_entity_translation_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity translation deletion.
hook_entity_translation_insert core/lib/Drupal/Core/Entity/entity.api.php Respond to creation of a new entity translation.
hook_ENTITY_TYPE_build_defaults_alter core/lib/Drupal/Core/Entity/entity.api.php Alter entity renderable values before cache checking in drupal_render().
hook_ENTITY_TYPE_create core/lib/Drupal/Core/Entity/entity.api.php Acts when creating a new entity of a specific type.
hook_ENTITY_TYPE_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity deletion of a particular type.
hook_ENTITY_TYPE_field_values_init core/lib/Drupal/Core/Entity/entity.api.php Acts when initializing a fieldable entity object.
hook_ENTITY_TYPE_insert core/lib/Drupal/Core/Entity/entity.api.php Respond to creation of a new entity of a particular type.
hook_ENTITY_TYPE_load core/lib/Drupal/Core/Entity/entity.api.php Act on entities of a specific type when loaded.
hook_ENTITY_TYPE_predelete core/lib/Drupal/Core/Entity/entity.api.php Act before entity deletion of a particular entity type.
hook_ENTITY_TYPE_prepare_form core/lib/Drupal/Core/Entity/entity.api.php Acts on a particular type of entity object about to be in an entity form.
hook_ENTITY_TYPE_presave core/lib/Drupal/Core/Entity/entity.api.php Act on a specific type of entity before it is created or updated.
hook_ENTITY_TYPE_revision_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity revision deletion of a particular type.
hook_ENTITY_TYPE_translation_create core/lib/Drupal/Core/Entity/entity.api.php Acts when creating a new entity translation of a specific type.
hook_ENTITY_TYPE_translation_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity translation deletion of a particular type.
hook_ENTITY_TYPE_translation_insert core/lib/Drupal/Core/Entity/entity.api.php Respond to creation of a new entity translation of a particular type.
hook_ENTITY_TYPE_update core/lib/Drupal/Core/Entity/entity.api.php Respond to updates to an entity of a particular type.
hook_ENTITY_TYPE_view core/lib/Drupal/Core/Entity/entity.api.php Act on entities of a particular type being assembled before rendering.
hook_ENTITY_TYPE_view_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the results of the entity build array for a particular entity type.
hook_entity_update core/lib/Drupal/Core/Entity/entity.api.php Respond to updates to an entity.
hook_entity_view core/lib/Drupal/Core/Entity/entity.api.php Act on entities being assembled before rendering.
hook_entity_view_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the results of the entity build array.
hook_entity_view_display_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the settings used for displaying an entity.
hook_entity_view_mode_alter core/lib/Drupal/Core/Entity/entity.api.php Change the view mode of an entity that is being displayed.
hook_node_search_result core/modules/node/node.api.php Act on a node being displayed as a search result.
hook_node_update_index core/modules/node/node.api.php Act on a node being indexed for searching.
hook_ranking core/modules/node/node.api.php Provide additional methods of scoring for core search results for nodes.

© 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_crud/8.1.x