function hook_entity_create

Act on a newly created entity.

This hook runs after a new entity object has just been instantiated. It can be used to set initial values, e.g. to provide defaults.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity object.

Related topics

2 functions implement hook_entity_create()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

entity_crud_hook_test_entity_create in drupal/core/modules/system/tests/modules/entity_crud_hook_test/entity_crud_hook_test.module
Implements hook_entity_create().
field_entity_create in drupal/core/modules/field/field.module
Implements hook_entity_create().
3 invocations of hook_entity_create()
ConfigStorageController::create in drupal/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php
Implements Drupal\Core\Entity\EntityStorageControllerInterface::create().
DatabaseStorageController::create in drupal/core/lib/Drupal/Core/Entity/DatabaseStorageController.php
Implements \Drupal\Core\Entity\EntityStorageControllerInterface::create().
DatabaseStorageControllerNG::create in drupal/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php
Overrides DatabaseStorageController::create().

File

drupal/core/includes/entity.api.php, line 173
Hooks provided the Entity module.

Code

function hook_entity_create(\Drupal\Core\Entity\EntityInterface $entity) {

  // @todo Remove the check for EntityNG once all entity types have been
  //   converted to it.
  if (!isset($entity->foo) && $entity instanceof \Drupal\Core\Entity\EntityNG) {
    $entity->foo->value = 'some_initial_value';
  }
}