3 namespace Drupal\Core\Plugin\Context;
5 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6 use Drupal\Core\TypedData\TypedDataTrait;
9 * Defines a class for context definitions.
11 class ContextDefinition implements ContextDefinitionInterface {
13 use DependencySerializationTrait;
18 * The data type of the data.
26 * The human-readable label.
34 * The human-readable description.
37 * The description, or NULL if no description is available.
39 protected $description;
42 * Whether the data is multi-valued, i.e. a list of data items.
46 protected $isMultiple = FALSE;
49 * Determines whether a data value is required.
52 * Whether a data value is required.
54 protected $isRequired = TRUE;
61 protected $defaultValue;
64 * An array of constraints.
68 protected $constraints = [];
71 * Creates a new context definition.
73 * @param string $data_type
74 * The data type for which to create the context definition. Defaults to
78 * The created context definition object.
80 public static function create($data_type = 'any') {
87 * Constructs a new context definition object.
89 * @param string $data_type
90 * The required data type.
91 * @param string|null $label
92 * The label of this context definition for the UI.
93 * @param bool $required
94 * Whether the context definition is required.
95 * @param bool $multiple
96 * Whether the context definition is multivalue.
97 * @param string|null $description
98 * The description of this context definition for the UI.
99 * @param mixed $default_value
100 * The default value of this definition.
102 public function __construct($data_type = 'any', $label = NULL, $required = TRUE, $multiple = FALSE, $description = NULL, $default_value = NULL) {
103 $this->dataType = $data_type;
104 $this->label = $label;
105 $this->isRequired = $required;
106 $this->isMultiple = $multiple;
107 $this->description = $description;
108 $this->defaultValue = $default_value;
114 public function getDataType() {
115 return $this->dataType;
121 public function setDataType($data_type) {
122 $this->dataType = $data_type;
129 public function getLabel() {
136 public function setLabel($label) {
137 $this->label = $label;
144 public function getDescription() {
145 return $this->description;
151 public function setDescription($description) {
152 $this->description = $description;
159 public function isMultiple() {
160 return $this->isMultiple;
166 public function setMultiple($multiple = TRUE) {
167 $this->isMultiple = $multiple;
174 public function isRequired() {
175 return $this->isRequired;
181 public function setRequired($required = TRUE) {
182 $this->isRequired = $required;
189 public function getDefaultValue() {
190 return $this->defaultValue;
196 public function setDefaultValue($default_value) {
197 $this->defaultValue = $default_value;
204 public function getConstraints() {
205 // @todo Apply defaults.
206 return $this->constraints;
212 public function getConstraint($constraint_name) {
213 $constraints = $this->getConstraints();
214 return isset($constraints[$constraint_name]) ? $constraints[$constraint_name] : NULL;
220 public function setConstraints(array $constraints) {
221 $this->constraints = $constraints;
228 public function addConstraint($constraint_name, $options = NULL) {
229 $this->constraints[$constraint_name] = $options;
236 public function getDataDefinition() {
237 if ($this->isMultiple()) {
238 $definition = $this->getTypedDataManager()->createListDataDefinition($this->getDataType());
241 $definition = $this->getTypedDataManager()->createDataDefinition($this->getDataType());
245 throw new \Exception("The data type '{$this->getDataType()}' is invalid");
247 $definition->setLabel($this->getLabel())
248 ->setDescription($this->getDescription())
249 ->setRequired($this->isRequired());
250 $constraints = $definition->getConstraints() + $this->getConstraints();
251 $definition->setConstraints($constraints);