class TaxonomyTermHierarchyConstraintValidator
Constraint validator for changing term parents in pending revisions.
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
- class \Drupal\taxonomy\Plugin\Validation\Constraint\TaxonomyTermHierarchyConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements \Drupal\Core\DependencyInjection\ContainerInjectionInterface
Expanded class hierarchy of TaxonomyTermHierarchyConstraintValidator
File
-
core/
modules/ taxonomy/ src/ Plugin/ Validation/ Constraint/ TaxonomyTermHierarchyConstraintValidator.php, line 15
Namespace
Drupal\taxonomy\Plugin\Validation\ConstraintView source
class TaxonomyTermHierarchyConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
/**
* Creates a new TaxonomyTermHierarchyConstraintValidator instance.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function validate($entity, Constraint $constraint) : void {
$term_storage = $this->entityTypeManager
->getStorage($entity->getEntityTypeId());
assert($term_storage instanceof TermStorageInterface);
// Newly created entities should be able to specify a parent.
if ($entity && $entity->isNew()) {
return;
}
$is_pending_revision = !$entity->isDefaultRevision();
$pending_term_ids = $term_storage->getTermIdsWithPendingRevisions();
$ancestors = $term_storage->loadAllParents($entity->id());
$ancestor_is_pending_revision = (bool) array_intersect_key($ancestors, array_flip($pending_term_ids));
$new_parents = array_column($entity->parent
->getValue(), 'target_id');
$original_parents = array_keys($term_storage->loadParents($entity->id())) ?: [
0,
];
if (($is_pending_revision || $ancestor_is_pending_revision) && $new_parents != $original_parents) {
$this->context
->buildViolation($constraint->message)
->atPath('parent')
->addViolation();
}
$original = $term_storage->loadUnchanged($entity->id());
if (($is_pending_revision || $ancestor_is_pending_revision) && !$entity->weight
->equals($original->weight)) {
$this->context
->buildViolation($constraint->message)
->atPath('weight')
->addViolation();
}
}
}
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"). |
|
TaxonomyTermHierarchyConstraintValidator::$entityTypeManager | private | property | The entity type manager. | |
TaxonomyTermHierarchyConstraintValidator::create | public static | function | Instantiates a new instance of this class. | Overrides ContainerInjectionInterface::create |
TaxonomyTermHierarchyConstraintValidator::validate | public | function | Checks if the passed value is valid. | Overrides ConstraintValidatorInterface::validate |
TaxonomyTermHierarchyConstraintValidator::__construct | public | function | Creates a new TaxonomyTermHierarchyConstraintValidator instance. |