function EntityBundleExistsConstraintValidator::validate

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EntityBundleExistsConstraintValidator.php \Drupal\Core\Validation\Plugin\Validation\Constraint\EntityBundleExistsConstraintValidator::validate()

File

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

Class

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

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {
    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,
        ]);
    }
}

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