3 namespace Drupal\Core\Entity\Plugin\Validation\Constraint;
5 use Symfony\Component\Validator\Constraint;
6 use Symfony\Component\Validator\ConstraintValidator;
9 * Validates the EntityChanged constraint.
11 class EntityChangedConstraintValidator extends ConstraintValidator {
16 public function validate($entity, Constraint $constraint) {
18 /** @var \Drupal\Core\Entity\EntityInterface $entity */
19 if (!$entity->isNew()) {
20 $saved_entity = \Drupal::entityManager()->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
21 // Ensure that all the entity translations are the same as or newer
22 // than their current version in the storage in order to avoid
23 // reverting other changes. In fact the entity object that is being
24 // saved might contain an older entity translation when different
25 // translations are being concurrently edited.
27 $common_translation_languages = array_intersect_key($entity->getTranslationLanguages(), $saved_entity->getTranslationLanguages());
28 foreach (array_keys($common_translation_languages) as $langcode) {
29 // Merely comparing the latest changed timestamps across all
30 // translations is not sufficient since other translations may have
31 // been edited and saved in the meanwhile. Therefore, compare the
32 // changed timestamps of each entity translation individually.
33 if ($saved_entity->getTranslation($langcode)->getChangedTime() > $entity->getTranslation($langcode)->getChangedTime()) {
34 $this->context->addViolation($constraint->message);