class BundleConstraint

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraint.php \Drupal\Core\Entity\Plugin\Validation\Constraint\BundleConstraint
  2. 10 core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraint.php \Drupal\Core\Entity\Plugin\Validation\Constraint\BundleConstraint
  3. 9 core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraint.php \Drupal\Core\Entity\Plugin\Validation\Constraint\BundleConstraint
  4. 8.9.x core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraint.php \Drupal\Core\Entity\Plugin\Validation\Constraint\BundleConstraint

Checks if a value is a valid entity type.

This differs from the `EntityBundleExists` constraint in that checks that the validated value is an *entity* of a particular bundle.

Attributes

#[Constraint(id: 'Bundle', label: new TranslatableMarkup('Bundle', [], [ 'context' => 'Validation', ]), type: [ 'entity', 'entity_reference', ])]

Hierarchy

  • class \Drupal\Core\Entity\Plugin\Validation\Constraint\BundleConstraint extends \Symfony\Component\Validator\Constraint

Expanded class hierarchy of BundleConstraint

2 files declare their use of BundleConstraint
ConstraintFactoryTest.php in core/tests/Drupal/KernelTests/Core/Validation/ConstraintFactoryTest.php
EntityContextDefinition.php in core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php

File

core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraint.php, line 16

Namespace

Drupal\Core\Entity\Plugin\Validation\Constraint
View source
class BundleConstraint extends SymfonyConstraint {
  
  /**
   * The bundle option.
   *
   * @var string|array
   */
  public $bundle;
  public function __construct(mixed $options = NULL, string|array|null $bundle = NULL, public $message = 'The entity must be of bundle %bundle.', ?array $groups = NULL, mixed $payload = NULL) {
    parent::__construct($options, $groups, $payload);
    $this->bundle = $bundle ?? $this->bundle;
  }
  
  /**
   * Gets the bundle option as array.
   *
   * @return array
   *   An array of bundle options.
   */
  public function getBundleOption() {
    // Support passing the bundle as string, but force it to be an array.
    if (!is_array($this->bundle)) {
      $this->bundle = [
        $this->bundle,
      ];
    }
    return $this->bundle;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getDefaultOption() : ?string {
    return 'bundle';
  }
  
  /**
   * {@inheritdoc}
   */
  public function getRequiredOptions() : array {
    return [
      'bundle',
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary
BundleConstraint::$bundle public property The bundle option.
BundleConstraint::getBundleOption public function Gets the bundle option as array.
BundleConstraint::getDefaultOption public function
BundleConstraint::getRequiredOptions public function
BundleConstraint::__construct public function #[HasNamedArguments]

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