function hook_entity_presave

Same name and namespace 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->getOriginal() 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

1 string reference to 'hook_entity_presave'
WorkspacesTestHooks::entityPresave in core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
Implements hook_entity_presave().
11 functions implement hook_entity_presave()

Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.

ContentModerationHooks::entityPresave in core/modules/content_moderation/src/Hook/ContentModerationHooks.php
Implements hook_entity_presave().
ContentTranslationHooks::entityPresave in core/modules/content_translation/src/Hook/ContentTranslationHooks.php
Implements hook_entity_presave().
EntityCrudHookTestHooks::entityPresave in core/modules/system/tests/modules/entity_crud_hook_test/src/Hook/EntityCrudHookTestHooks.php
Implements hook_entity_presave().
EntityOperations::entityPresave in core/modules/workspaces/src/Hook/EntityOperations.php
Implements hook_entity_presave().
EntityTestHooks::entityPresave in core/modules/system/tests/modules/entity_test/src/Hook/EntityTestHooks.php
Implements hook_entity_presave().

... See full list

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 1168

Code

function hook_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) : void {
  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.