You are here

public function GroupSubgroupConstraintValidator::validate in Subgroup (Graph) 1.0.x

File

src/Plugin/Validation/Constraint/GroupSubgroupConstraintValidator.php, line 53

Class

GroupSubgroupConstraintValidator
Validates the GroupSubgroup constraint.

Namespace

Drupal\ggroup\Plugin\Validation\Constraint

Code

public function validate($entity, Constraint $constraint) {
  if (!isset($entity)) {
    return;
  }
  if (!$entity instanceof GroupContentInterface) {
    return;
  }
  if ($entity
    ->getContentPlugin()
    ->getEntityTypeId() !== 'group') {
    return;
  }
  $parent_group = $entity
    ->getGroup();
  $child_group = $entity
    ->getEntity();

  // If the child group already has the parent group as a subgroup, then
  // adding the relationship will cause a circular reference.
  if ($parent_group && $child_group && $this->groupHierarchyManager
    ->groupHasSubgroup($child_group, $parent_group)) {
    $this->context
      ->buildViolation($constraint->message)
      ->setParameter('%parent_group_label', $parent_group
      ->label())
      ->setParameter('%child_group_label', $child_group
      ->label())
      ->addViolation();
  }
}