BundleConstraint.php

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

Namespace

Drupal\Core\Entity\Plugin\Validation\Constraint

File

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

View source
<?php

namespace Drupal\Core\Entity\Plugin\Validation\Constraint;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Validation\Attribute\Constraint;
use Symfony\Component\Validator\Constraint as SymfonyConstraint;

/**
 * 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.
 */
class BundleConstraint extends SymfonyConstraint {
  public function __construct(public string|array $bundle, public $message = 'The entity must be of bundle %bundle.', ?array $groups = NULL, mixed $payload = NULL) {
    parent::__construct(groups: $groups, payload: $payload);
  }
  
  /**
   * 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;
  }

}

Classes

Title Deprecated Summary
BundleConstraint Checks if a value is a valid entity type.

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