function MenuTreeHierarchyConstraintValidator::validate

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::validate()
  2. 8.9.x core/modules/menu_link_content/src/Plugin/Validation/Constraint/MenuTreeHierarchyConstraintValidator.php \Drupal\menu_link_content\Plugin\Validation\Constraint\MenuTreeHierarchyConstraintValidator::validate()
  3. 10 core/modules/menu_link_content/src/Plugin/Validation/Constraint/MenuTreeHierarchyConstraintValidator.php \Drupal\menu_link_content\Plugin\Validation\Constraint\MenuTreeHierarchyConstraintValidator::validate()

File

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

Class

MenuTreeHierarchyConstraintValidator
Constraint validator for changing menu link parents in pending revisions.

Namespace

Drupal\menu_link_content\Plugin\Validation\Constraint

Code

public function validate($entity, Constraint $constraint) : void {
    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();
        }
    }
}

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