Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_ENTITY_TYPE_presave()
  2. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_ENTITY_TYPE_presave()

Act on a specific type of 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_presave()

Related topics

38 functions implement hook_ENTITY_TYPE_presave()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

block_content_test_block_content_presave in core/modules/block_content/tests/modules/block_content_test/block_content_test.module
Implements hook_block_content_presave().
book_node_presave in core/modules/book/book.module
Implements hook_ENTITY_TYPE_presave() for node entities.
ckeditor5_editor_presave in core/modules/ckeditor5/ckeditor5.module
Implements hook_ENTITY_TYPE_presave() for editor entities.
comment_entity_view_display_presave in core/modules/comment/comment.module
Implements hook_ENTITY_TYPE_presave() for entity_view_display entities.
config_test_config_test_presave in core/modules/config/tests/config_test/config_test.hooks.inc
Implements hook_config_test_presave().

... See full list

File

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

Code

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