class MenuTreeHierarchyConstraintValidator

Same name in other branches
  1. 9 core/modules/menu_link_content/src/Plugin/Validation/Constraint/MenuTreeHierarchyConstraintValidator.php \Drupal\menu_link_content\Plugin\Validation\Constraint\MenuTreeHierarchyConstraintValidator
  2. 8.9.x core/modules/menu_link_content/src/Plugin/Validation/Constraint/MenuTreeHierarchyConstraintValidator.php \Drupal\menu_link_content\Plugin\Validation\Constraint\MenuTreeHierarchyConstraintValidator
  3. 11.x core/modules/menu_link_content/src/Plugin/Validation/Constraint/MenuTreeHierarchyConstraintValidator.php \Drupal\menu_link_content\Plugin\Validation\Constraint\MenuTreeHierarchyConstraintValidator

Constraint validator for changing menu link parents in pending revisions.

Hierarchy

Expanded class hierarchy of MenuTreeHierarchyConstraintValidator

File

core/modules/menu_link_content/src/Plugin/Validation/Constraint/MenuTreeHierarchyConstraintValidator.php, line 14

Namespace

Drupal\menu_link_content\Plugin\Validation\Constraint
View source
class MenuTreeHierarchyConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    private $entityTypeManager;
    
    /**
     * Creates a new MenuTreeHierarchyConstraintValidator 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) {
        if ($entity && !$entity->isNew() && !$entity->isDefaultRevision()) {
            $original = $this->entityTypeManager
                ->getStorage($entity->getEntityTypeId())
                ->loadUnchanged($entity->id());
            // Ensure that empty items do not affect the comparison checks below.
            // @todo Remove this filtering when
            //   https://www.drupal.org/project/drupal/issues/3039031 is fixed.
            $entity->parent
                ->filterEmptyItems();
            if ($entity->parent
                ->isEmpty() !== $original->parent
                ->isEmpty() || !$entity->parent
                ->equals($original->parent)) {
                $this->context
                    ->buildViolation($constraint->message)
                    ->atPath('menu_parent')
                    ->addViolation();
            }
            if (!$entity->weight
                ->equals($original->weight)) {
                $this->context
                    ->buildViolation($constraint->message)
                    ->atPath('weight')
                    ->addViolation();
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
MenuTreeHierarchyConstraintValidator::$entityTypeManager private property The entity type manager.
MenuTreeHierarchyConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
MenuTreeHierarchyConstraintValidator::validate public function
MenuTreeHierarchyConstraintValidator::__construct public function Creates a new MenuTreeHierarchyConstraintValidator instance.

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