You are here

class ValidOgMembershipReferenceConstraintValidator in Organic groups 8

Checks if referenced entities are valid.

Hierarchy

Expanded class hierarchy of ValidOgMembershipReferenceConstraintValidator

File

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

Namespace

Drupal\og\Plugin\Validation\Constraint
View source
class ValidOgMembershipReferenceConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

}

Members