You are here

public function ValidOgMembershipReferenceConstraintValidator::validate in Organic groups 8

File

src/Plugin/Validation/Constraint/ValidOgMembershipReferenceConstraintValidator.php, line 19

Class

ValidOgMembershipReferenceConstraintValidator
Checks if referenced entities are valid.

Namespace

Drupal\og\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {

  /** @var \Drupal\Core\Field\FieldItemInterface $value */
  if (!isset($value)) {
    return;
  }
  $entity = \Drupal::entityTypeManager()
    ->getStorage($value
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getSetting('target_type'))
    ->load($value
    ->get('target_id')
    ->getValue());
  if (!$entity) {

    // Entity with that entity ID does not exists. This could happen if a
    // stale entity is passed for validation.
    return;
  }
  $params['%label'] = $entity
    ->label();
  if (!Og::isGroup($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    $this->context
      ->addViolation($constraint->notValidGroup, $params);
  }
}