hook_entity_field_values_init

hook_entity_field_values_init(\Drupal\Core\Entity\FieldableEntityInterface $entity)

Acts when initializing a fieldable entity object.

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

Parameters

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

See also

hook_ENTITY_TYPE_field_values_init()

Related topics

Entity CRUD, editing, and view hooks
Hooks used in various entity operations.
Hooks
Define functions that alter the behavior of Drupal core.

File

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

Code

function hook_entity_field_values_init(\Drupal\Core\Entity\FieldableEntityInterface $entity) {
  if ($entity instanceof \Drupal\Core\Entity\ContentEntityInterface && !$entity->foo->value) {
    $entity->foo->value = 'some_initial_value';
  }
}
doc_Drupal
2016-10-29 09:17:59
Comments
Leave a Comment

Please login to continue.