Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Plugin / Validation / Constraint / EntityChangedConstraintValidator.php
1 <?php
2
3 namespace Drupal\Core\Entity\Plugin\Validation\Constraint;
4
5 use Symfony\Component\Validator\Constraint;
6 use Symfony\Component\Validator\ConstraintValidator;
7
8 /**
9  * Validates the EntityChanged constraint.
10  */
11 class EntityChangedConstraintValidator extends ConstraintValidator {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function validate($entity, Constraint $constraint) {
17     if (isset($entity)) {
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.
26         if ($saved_entity) {
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);
35               break;
36             }
37           }
38         }
39       }
40     }
41   }
42
43 }