You are here

public function MenuTreeHierarchyConstraintValidator::validate in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/menu_link_content/src/Plugin/Validation/Constraint/MenuTreeHierarchyConstraintValidator.php \Drupal\menu_link_content\Plugin\Validation\Constraint\MenuTreeHierarchyConstraintValidator::validate()
  2. 9 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) {
  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();
    }
  }
}