function hook_entity_presave

Same name in other branches
  1. 7.x modules/system/system.api.php \hook_entity_presave()
  2. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_presave()
  3. 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_presave()
  4. 10 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_presave()

Act on an entity before it is created or updated.

You can get the original entity object from $entity->original when it is an update of the entity.

Parameters

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

See also

hook_ENTITY_TYPE_presave()

Related topics

2 functions implement 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 core/modules/system/tests/modules/entity_crud_hook_test/entity_crud_hook_test.module
Implements hook_entity_presave().
jsonapi_test_non_cacheable_methods_entity_presave in core/modules/jsonapi/tests/modules/jsonapi_test_non_cacheable_methods/jsonapi_test_non_cacheable_methods.module
Implements hook_entity_presave().
1 invocation of hook_entity_presave()
EntityStorageBase::doPreSave in core/lib/Drupal/Core/Entity/EntityStorageBase.php
Performs presave entity processing.

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1137

Code

function hook_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {
    if ($entity instanceof ContentEntityInterface && $entity->isTranslatable()) {
        $route_match = \Drupal::routeMatch();
        \Drupal::service('content_translation.synchronizer')->synchronizeFields($entity, $entity->language()
            ->getId(), $route_match->getParameter('source_langcode'));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.