class LangcodeRequiredIfTranslatableValuesConstraintValidator
Validates the LangcodeRequiredIfTranslatableValues constraint.
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
- class \Drupal\Core\Config\Plugin\Validation\Constraint\LangcodeRequiredIfTranslatableValuesConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of LangcodeRequiredIfTranslatableValuesConstraintValidator
File
-
core/
lib/ Drupal/ Core/ Config/ Plugin/ Validation/ Constraint/ LangcodeRequiredIfTranslatableValuesConstraintValidator.php, line 15
Namespace
Drupal\Core\Config\Plugin\Validation\ConstraintView source
final class LangcodeRequiredIfTranslatableValuesConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate(mixed $value, Constraint $constraint) : void {
assert($constraint instanceof LangcodeRequiredIfTranslatableValuesConstraint);
$mapping = $this->context
->getObject();
assert($mapping instanceof Mapping);
$root = $this->context
->getRoot();
if ($mapping !== $root) {
throw new LogicException(sprintf('The LangcodeRequiredIfTranslatableValues constraint is applied to \'%s\'. This constraint can only operate on the root object being validated.', $root->getName() . '::' . $mapping->getName()));
}
assert(in_array('langcode', $mapping->getValidKeys(), TRUE));
$is_translatable = $mapping->hasTranslatableElements();
if ($is_translatable && !array_key_exists('langcode', $value)) {
$this->context
->buildViolation($constraint->missingMessage)
->setParameter('@name', $mapping->getName())
->addViolation();
return;
}
if (!$is_translatable && array_key_exists('langcode', $value)) {
// @todo Convert this deprecation to an actual validation error in
// https://www.drupal.org/project/drupal/issues/3440238.
// phpcs:ignore
@trigger_error(str_replace('@name', $mapping->getName(), $constraint->superfluousMessage), E_USER_DEPRECATED);
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ConstraintValidator::$context | protected | property | ||
ConstraintValidator::formatTypeOf | protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator::formatValue | protected | function | Returns a string representation of the value. | |
ConstraintValidator::formatValues | protected | function | Returns a string representation of a list of values. | |
ConstraintValidator::initialize | public | function | Initializes the constraint validator. | Overrides ConstraintValidatorInterface::initialize |
ConstraintValidator::OBJECT_TO_STRING | public | constant | Whether to cast objects with a "__toString()" method to strings. | |
ConstraintValidator::PRETTY_DATE | public | constant | Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter} (if it is available) or as RFC-3339 dates ("Y-m-d H:i:s"). |
|
LangcodeRequiredIfTranslatableValuesConstraintValidator::validate | public | function | Checks if the passed value is valid. | Overrides ConstraintValidatorInterface::validate |