You are here

ValidOgMembershipReferenceConstraintValidator.php in Organic groups 8

File

src/Plugin/Validation/Constraint/ValidOgMembershipReferenceConstraintValidator.php
View source
<?php

declare (strict_types=1);
namespace Drupal\og\Plugin\Validation\Constraint;

use Drupal\og\Og;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * Checks if referenced entities are valid.
 */
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);
    }
  }

}

Classes

Namesort descending Description
ValidOgMembershipReferenceConstraintValidator Checks if referenced entities are valid.