W3cubDocs

/Drupal 8

function hook_entity_presave

hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity)

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

Entity CRUD, editing, and view hooks
Hooks used in various entity operations.
Hooks
Define functions that alter the behavior of Drupal core.

File

core/lib/Drupal/Core/Entity/entity.api.php, line 895
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'));
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!entity.api.php/function/hook_entity_presave/8.1.x