You are here

class ContextDefinition in Typed Data API enhancements 8

Same name in this branch
  1. 8 src/Context/ContextDefinition.php \Drupal\typed_data\Context\ContextDefinition
  2. 8 src/Context/Annotation/ContextDefinition.php \Drupal\typed_data\Context\Annotation\ContextDefinition

Extends the core context definition class with useful methods.

Hierarchy

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\Context
View 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

Namesort descending Modifiers Type Description Overrides
ContextDefinition::$allowNull protected property Whether the context value is allowed to be NULL or not.
ContextDefinition::$assignmentRestriction protected property The assignment restriction of this context.
ContextDefinition::$constraints protected property An array of constraints.
ContextDefinition::$dataType protected property The data type of the data.
ContextDefinition::$defaultValue protected property The default value.
ContextDefinition::$description protected property The human-readable description.
ContextDefinition::$entityContextDefinition Deprecated private property An EntityContextDefinition instance, for backwards compatibility.
ContextDefinition::$isMultiple protected property Whether the data is multi-valued, i.e. a list of data items.
ContextDefinition::$isRequired protected property Determines whether a data value is required.
ContextDefinition::$label protected property The human-readable label.
ContextDefinition::$nameMap protected static property The mapping of config export keys to internal properties.
ContextDefinition::addConstraint public function Adds a validation constraint. Overrides ContextDefinitionInterface::addConstraint
ContextDefinition::create public static function Creates a new context definition.
ContextDefinition::createFromArray public static function Creates a definition object from an exported array of values.
ContextDefinition::dataTypeMatches protected function Checks if this definition's data type matches that of the given context.
ContextDefinition::getAssignmentRestriction public function Determines if this context has an assignment restriction. Overrides ContextDefinitionInterface::getAssignmentRestriction
ContextDefinition::getConstraint public function Gets a validation constraint. Overrides ContextDefinitionInterface::getConstraint
ContextDefinition::getConstraintObjects protected function Extracts an array of constraints for a context definition object. 1
ContextDefinition::getConstraints public function Gets an array of validation constraints. Overrides ContextDefinitionInterface::getConstraints
ContextDefinition::getDataDefinition public function Returns the data definition of the defined context. Overrides ContextDefinitionInterface::getDataDefinition
ContextDefinition::getDataType public function Gets the data type needed by the context. Overrides ContextDefinitionInterface::getDataType
ContextDefinition::getDefaultValue public function Gets the default value for this context definition. Overrides ContextDefinitionInterface::getDefaultValue
ContextDefinition::getDescription public function Gets a human readable description. Overrides ContextDefinitionInterface::getDescription
ContextDefinition::getLabel public function Gets a human readable label. Overrides ContextDefinitionInterface::getLabel
ContextDefinition::getSampleValues protected function Returns typed data objects representing this context definition. 1
ContextDefinition::initializeEntityContextDefinition private function Initializes $this->entityContextDefinition for backwards compatibility.
ContextDefinition::isAllowedNull public function Determines if the context value is allowed to be NULL. Overrides ContextDefinitionInterface::isAllowedNull
ContextDefinition::isMultiple public function Determines whether the data is multi-valued, i.e. a list of data items. Overrides ContextDefinitionInterface::isMultiple
ContextDefinition::isRequired public function Determines whether the context is required. Overrides ContextDefinitionInterface::isRequired
ContextDefinition::isSatisfiedBy public function Determines if this definition is satisfied by a context object. Overrides ContextDefinitionInterface::isSatisfiedBy
ContextDefinition::setAllowNull public function Sets the "allow NULL value" behavior. Overrides ContextDefinitionInterface::setAllowNull
ContextDefinition::setAssignmentRestriction public function Sets the assignment restriction mode for this context. Overrides ContextDefinitionInterface::setAssignmentRestriction
ContextDefinition::setConstraints public function Sets the array of validation constraints. Overrides ContextDefinitionInterface::setConstraints
ContextDefinition::setDataType public function Sets the data type needed by the context. Overrides ContextDefinitionInterface::setDataType
ContextDefinition::setDefaultValue public function Sets the default data value. Overrides ContextDefinitionInterface::setDefaultValue
ContextDefinition::setDescription public function Sets the human readable description. Overrides ContextDefinitionInterface::setDescription
ContextDefinition::setLabel public function Sets the human readable label. Overrides ContextDefinitionInterface::setLabel
ContextDefinition::setMultiple public function Sets whether the data is multi-valued. Overrides ContextDefinitionInterface::setMultiple
ContextDefinition::setRequired public function Sets whether the data is required. Overrides ContextDefinitionInterface::setRequired
ContextDefinition::toArray public function Exports the definition as an array. Overrides ContextDefinitionInterface::toArray
ContextDefinition::__construct public function Constructs a new context definition object. 1
ContextDefinition::__sleep public function Implements magic __sleep() method.
ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_INPUT constant Constants for the context assignment restriction mode.
ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR constant
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function Aliased as: traitSleep 1
DependencySerializationTrait::__wakeup public function 2
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2