ContextDefinition.php in Zircon Profile 8.0
File
core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
View source
<?php
namespace Drupal\Core\Plugin\Context;
use Drupal\Core\TypedData\TypedDataTrait;
class ContextDefinition implements ContextDefinitionInterface {
use TypedDataTrait;
protected $dataType;
protected $label;
protected $description;
protected $isMultiple = FALSE;
protected $isRequired = TRUE;
protected $defaultValue;
protected $constraints = [];
public static function create($data_type = 'any') {
return new static($data_type);
}
public function __construct($data_type = 'any', $label = NULL, $required = TRUE, $multiple = FALSE, $description = NULL, $default_value = NULL) {
$this->dataType = $data_type;
$this->label = $label;
$this->isRequired = $required;
$this->isMultiple = $multiple;
$this->description = $description;
$this->defaultValue = $default_value;
}
public function getDataType() {
return $this->dataType;
}
public function setDataType($data_type) {
$this->dataType = $data_type;
return $this;
}
public function getLabel() {
return $this->label;
}
public function setLabel($label) {
$this->label = $label;
return $this;
}
public function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = $description;
return $this;
}
public function isMultiple() {
return $this->isMultiple;
}
public function setMultiple($multiple = TRUE) {
$this->isMultiple = $multiple;
return $this;
}
public function isRequired() {
return $this->isRequired;
}
public function setRequired($required = TRUE) {
$this->isRequired = $required;
return $this;
}
public function getDefaultValue() {
return $this->defaultValue;
}
public function setDefaultValue($default_value) {
$this->defaultValue = $default_value;
return $this;
}
public function getConstraints() {
return $this->constraints;
}
public function getConstraint($constraint_name) {
$constraints = $this
->getConstraints();
return isset($constraints[$constraint_name]) ? $constraints[$constraint_name] : NULL;
}
public function setConstraints(array $constraints) {
$this->constraints = $constraints;
return $this;
}
public function addConstraint($constraint_name, $options = NULL) {
$this->constraints[$constraint_name] = $options;
return $this;
}
public function getDataDefinition() {
if ($this
->isMultiple()) {
$definition = $this
->getTypedDataManager()
->createListDataDefinition($this
->getDataType());
}
else {
$definition = $this
->getTypedDataManager()
->createDataDefinition($this
->getDataType());
}
if (!$definition) {
throw new \Exception("The data type '{$this->getDataType()}' is invalid");
}
$definition
->setLabel($this
->getLabel())
->setDescription($this
->getDescription())
->setRequired($this
->isRequired());
$constraints = $definition
->getConstraints() + $this
->getConstraints();
$definition
->setConstraints($constraints);
return $definition;
}
}