BundleConstraint.php

Same filename and directory in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraint.php
  2. 10 core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraint.php
  3. 11.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 Symfony\Component\Validator\Constraint;

/**
 * Checks if a value is a valid entity type.
 *
 * @Constraint(
 *   id = "Bundle",
 *   label = @Translation("Bundle", context = "Validation"),
 *   type = { "entity", "entity_reference" }
 * )
 */
class BundleConstraint extends Constraint {
    
    /**
     * The default violation message.
     *
     * @var string
     */
    public $message = 'The entity must be of bundle %bundle.';
    
    /**
     * The bundle option.
     *
     * @var string|array
     */
    public $bundle;
    
    /**
     * Gets the bundle option as array.
     *
     * @return array
     */
    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',
        ];
    }

}

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.