class EntityBundleExistsConstraintValidator

Same name in other branches
  1. 10 core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EntityBundleExistsConstraintValidator.php \Drupal\Core\Validation\Plugin\Validation\Constraint\EntityBundleExistsConstraintValidator

Validates that a bundle exists on a certain content entity type.

Hierarchy

Expanded class hierarchy of EntityBundleExistsConstraintValidator

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EntityBundleExistsConstraintValidator.php, line 18

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint
View source
class EntityBundleExistsConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
    
    /**
     * Constructs an EntityBundleExistsConstraintValidator object.
     *
     * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInfo
     *   The entity type bundle info service.
     */
    public function __construct(EntityTypeBundleInfoInterface $bundleInfo) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get(EntityTypeBundleInfoInterface::class));
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint) : void {
        assert($constraint instanceof EntityBundleExistsConstraint);
        if (!is_string($value)) {
            throw new UnexpectedTypeException($value, 'string');
        }
        // Resolve any dynamic tokens, like %parent, in the entity type ID.
        $entity_type_id = TypeResolver::resolveDynamicTypeName("[{$constraint->entityTypeId}]", $this->context
            ->getObject());
        if (!array_key_exists($value, $this->bundleInfo
            ->getBundleInfo($entity_type_id))) {
            $this->context
                ->addViolation($constraint->message, [
                '@bundle' => $value,
                '@entity_type_id' => $entity_type_id,
            ]);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EntityBundleExistsConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityBundleExistsConstraintValidator::validate public function
EntityBundleExistsConstraintValidator::__construct public function Constructs an EntityBundleExistsConstraintValidator object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.