function ContentTranslationSynchronizedFieldsConstraintValidator::validate

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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);
        }
    }
}

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