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. 11.x 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

17 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().
content_moderation_entity_presave in core/modules/content_moderation/content_moderation.module
Implements hook_entity_presave().
content_translation_entity_presave in core/modules/content_translation/content_translation.module
Implements hook_entity_presave().
EntityCrudHookTestHooks::entityPresave in core/modules/system/tests/modules/entity_crud_hook_test/src/Hook/EntityCrudHookTestHooks.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 1135

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.