8.2.x entity.api.php hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity)
8.0.x entity.api.php hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity)
8.1.x entity.api.php hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity)
8.3.x entity.api.php hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity)
7.x system.api.php hook_entity_presave($entity, $type)

Act on an entity before it is about to be created or updated.


$entity: The entity object.

$type: The type of entity being saved (i.e. node, user, comment).

Related topics

1 function implements hook_entity_presave()

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_presave in modules/simpletest/tests/entity_crud_hook_test.module
Implements hook_entity_presave().
8 invocations of hook_entity_presave()
comment_save in modules/comment/comment.module
Accepts a submission of new or changed comment content.
field_attach_presave in modules/field/field.attach.inc
Perform necessary operations just before fields data get saved.
file_save in includes/file.inc
Saves a file object to the database.
node_save in modules/node/node.module
Saves changes to a node or adds a new node.
taxonomy_term_save in modules/taxonomy/taxonomy.module
Saves a term object to the database.

... See full list


modules/system/system.api.php, line 316
Hooks provided by Drupal core and the System module.


function hook_entity_presave($entity, $type) {
  $entity->changed = REQUEST_TIME;


nehapandya55’s picture

I have used this hook to update profile field data
* Implements hook_entity_presave().
function YOUR_MODULE_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityType()->id() == 'profile') {
$zipcode = $entity->field_zip_code->value;
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false";
$result = json_decode($details,true);
$entity->field_geolocation->lat = $lat;
$entity->field_geolocation->lng = $lng;