class ContextDefinition in Typed Data API enhancements 8
Same name in this branch
- 8 src/Context/ContextDefinition.php \Drupal\typed_data\Context\ContextDefinition
- 8 src/Context/Annotation/ContextDefinition.php \Drupal\typed_data\Context\Annotation\ContextDefinition
Extends the core context definition class with useful methods.
Hierarchy
- class \Drupal\Core\Plugin\Context\ContextDefinition implements ContextDefinitionInterface uses DependencySerializationTrait, TypedDataTrait
- class \Drupal\typed_data\Context\ContextDefinition implements ContextDefinitionInterface
Expanded class hierarchy of ContextDefinition
2 files declare their use of ContextDefinition
- ContextDefinition.php in src/
Context/ Annotation/ ContextDefinition.php - SelectWidget.php in src/
Plugin/ TypedDataFormWidget/ SelectWidget.php
File
- src/
Context/ ContextDefinition.php, line 11
Namespace
Drupal\typed_data\ContextView source
class ContextDefinition extends ContextDefinitionCore implements ContextDefinitionInterface {
/**
* The mapping of config export keys to internal properties.
*
* @var array
*/
protected static $nameMap = [
'type' => 'dataType',
'label' => 'label',
'description' => 'description',
'multiple' => 'isMultiple',
'required' => 'isRequired',
'default_value' => 'defaultValue',
'constraints' => 'constraints',
'allow_null' => 'allowNull',
'assignment_restriction' => 'assignmentRestriction',
];
/**
* Whether the context value is allowed to be NULL or not.
*
* @var bool
*/
protected $allowNull = FALSE;
/**
* The assignment restriction of this context.
*
* @var string|null
*
* @see \Drupal\typed_data\Context\ContextDefinitionInterface::getAssignmentRestriction()
*/
protected $assignmentRestriction = NULL;
/**
* {@inheritdoc}
*/
public function toArray() {
$values = [];
$defaults = get_class_vars(__CLASS__);
foreach (static::$nameMap as $key => $property_name) {
// Only export values for non-default properties.
if ($this->{$property_name} !== $defaults[$property_name]) {
$values[$key] = $this->{$property_name};
}
}
return $values;
}
/**
* Creates a definition object from an exported array of values.
*
* @param array $values
* The array of values, as returned by toArray().
*
* @return static
* The created definition.
*
* @throws \Drupal\Component\Plugin\Exception\ContextException
* If the required classes are not implemented.
*/
public static function createFromArray(array $values) {
if (isset($values['class']) && !in_array(ContextDefinitionInterface::class, class_implements($values['class']))) {
throw new ContextException('ContextDefinition class must implement ' . ContextDefinitionInterface::class . '.');
}
// Default to Typed Data context definition class.
$values['class'] = isset($values['class']) ? $values['class'] : ContextDefinition::class;
if (!isset($values['value'])) {
$values['value'] = 'any';
}
$definition = $values['class']::create($values['value']);
foreach (array_intersect_key(static::$nameMap, $values) as $key => $name) {
$definition->{$name} = $values[$key];
}
return $definition;
}
/**
* {@inheritdoc}
*/
public function isAllowedNull() {
return $this->allowNull;
}
/**
* {@inheritdoc}
*/
public function setAllowNull($null_allowed) {
$this->allowNull = $null_allowed;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAssignmentRestriction() {
return $this->assignmentRestriction;
}
/**
* {@inheritdoc}
*/
public function setAssignmentRestriction($restriction) {
$this->assignmentRestriction = $restriction;
return $this;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContextDefinition:: |
protected | property | Whether the context value is allowed to be NULL or not. | |
ContextDefinition:: |
protected | property | The assignment restriction of this context. | |
ContextDefinition:: |
protected | property | An array of constraints. | |
ContextDefinition:: |
protected | property | The data type of the data. | |
ContextDefinition:: |
protected | property | The default value. | |
ContextDefinition:: |
protected | property | The human-readable description. | |
ContextDefinition:: |
private | property | An EntityContextDefinition instance, for backwards compatibility. | |
ContextDefinition:: |
protected | property | Whether the data is multi-valued, i.e. a list of data items. | |
ContextDefinition:: |
protected | property | Determines whether a data value is required. | |
ContextDefinition:: |
protected | property | The human-readable label. | |
ContextDefinition:: |
protected static | property | The mapping of config export keys to internal properties. | |
ContextDefinition:: |
public | function |
Adds a validation constraint. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public static | function | Creates a new context definition. | |
ContextDefinition:: |
public static | function | Creates a definition object from an exported array of values. | |
ContextDefinition:: |
protected | function | Checks if this definition's data type matches that of the given context. | |
ContextDefinition:: |
public | function |
Determines if this context has an assignment restriction. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Gets a validation constraint. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
protected | function | Extracts an array of constraints for a context definition object. | 1 |
ContextDefinition:: |
public | function |
Gets an array of validation constraints. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Returns the data definition of the defined context. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Gets the data type needed by the context. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Gets the default value for this context definition. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Gets a human readable description. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Gets a human readable label. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
protected | function | Returns typed data objects representing this context definition. | 1 |
ContextDefinition:: |
private | function | Initializes $this->entityContextDefinition for backwards compatibility. | |
ContextDefinition:: |
public | function |
Determines if the context value is allowed to be NULL. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Determines whether the data is multi-valued, i.e. a list of data items. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Determines whether the context is required. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Determines if this definition is satisfied by a context object. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Sets the "allow NULL value" behavior. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Sets the assignment restriction mode for this context. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Sets the array of validation constraints. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Sets the data type needed by the context. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Sets the default data value. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Sets the human readable description. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Sets the human readable label. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Sets whether the data is multi-valued. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Sets whether the data is required. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function |
Exports the definition as an array. Overrides ContextDefinitionInterface:: |
|
ContextDefinition:: |
public | function | Constructs a new context definition object. | 1 |
ContextDefinition:: |
public | function | Implements magic __sleep() method. | |
ContextDefinitionInterface:: |
constant | Constants for the context assignment restriction mode. | ||
ContextDefinitionInterface:: |
constant | |||
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | Aliased as: traitSleep | 1 |
DependencySerializationTrait:: |
public | function | 2 | |
TypedDataTrait:: |
protected | property | The typed data manager used for creating the data types. | |
TypedDataTrait:: |
public | function | Gets the typed data manager. | 2 |
TypedDataTrait:: |
public | function | Sets the typed data manager. | 2 |