You are here

public function ContentTranslationSynchronizedFieldsConstraintValidator::validate in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraintValidator.php \Drupal\content_translation\Plugin\Validation\Constraint\ContentTranslationSynchronizedFieldsConstraintValidator::validate()
  2. 10 core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraintValidator.php \Drupal\content_translation\Plugin\Validation\Constraint\ContentTranslationSynchronizedFieldsConstraintValidator::validate()

File

core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraintValidator.php, line 88

Class

ContentTranslationSynchronizedFieldsConstraintValidator
Checks that synchronized fields are handled correctly in pending revisions.

Namespace

Drupal\content_translation\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {

  /** @var \Drupal\content_translation\Plugin\Validation\Constraint\ContentTranslationSynchronizedFieldsConstraint $constraint */

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $value;
  if ($entity
    ->isNew() || !$entity
    ->getEntityType()
    ->isRevisionable()) {
    return;
  }

  // When changes to untranslatable fields are configured to affect all
  // revision translations, we always allow changes in default revisions.
  if ($entity
    ->isDefaultRevision() && !$entity
    ->isDefaultTranslationAffectedOnly()) {
    return;
  }
  $entity_type_id = $entity
    ->getEntityTypeId();
  if (!$this->contentTranslationManager
    ->isEnabled($entity_type_id, $entity
    ->bundle())) {
    return;
  }
  $synchronized_properties = $this
    ->getSynchronizedPropertiesByField($entity
    ->getFieldDefinitions());
  if (!$synchronized_properties) {
    return;
  }

  /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
  $original = $this
    ->getOriginalEntity($entity);
  $original_translation = $this
    ->getOriginalTranslation($entity, $original);
  if ($this
    ->hasSynchronizedPropertyChanges($entity, $original_translation, $synchronized_properties)) {
    if ($entity
      ->isDefaultTranslationAffectedOnly()) {
      foreach ($entity
        ->getTranslationLanguages(FALSE) as $langcode => $language) {
        if ($entity
          ->getTranslation($langcode)
          ->hasTranslationChanges()) {
          $this->context
            ->addViolation($constraint->defaultTranslationMessage);
          break;
        }
      }
    }
    else {
      $this->context
        ->addViolation($constraint->defaultRevisionMessage);
    }
  }
}