You are here

function hook_entity_presave in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_presave()
  2. 7 modules/system/system.api.php \hook_entity_presave()
  3. 9 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

8 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.

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().
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().
entity_test_entity_presave in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_entity_presave().
image_entity_presave in core/modules/image/image.module
Implements hook_entity_presave().

... See full list

3 invocations of hook_entity_presave()
ConfigEntityStorageTest::testSaveInsert in core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php
@covers ::save @covers ::doSave
ConfigEntityStorageTest::testSaveUpdate in core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php
@covers ::save @covers ::doSave
EntityStorageBase::doPreSave in core/lib/Drupal/Core/Entity/EntityStorageBase.php
Performs presave entity processing.

File

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

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'));
  }
}