function hook_ENTITY_TYPE_presave

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

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

comment_entity_view_display_presave in core/modules/comment/comment.module
Implements hook_ENTITY_TYPE_presave() for entity_view_display entities.
field_ui_entity_form_mode_presave in core/modules/field_ui/field_ui.module
Implements hook_entity_form_mode_presave().
field_ui_entity_view_mode_presave in core/modules/field_ui/field_ui.module
Implements hook_entity_view_mode_presave().

File

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

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

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