class EntityContextDefinition

Extends the core entity context definition class with useful methods.

Warning: Do not instantiate this directly in your code. This class is only meant to be used from \Drupal\rules\Context\ContextDefinition. Please read the API documentation for that class and see the links below for details.

@internal

Hierarchy

Expanded class hierarchy of EntityContextDefinition

See also

\Drupal\rules\Context\ContextDefinition

https://www.drupal.org/project/rules/issues/3161582

https://www.drupal.org/project/drupal/issues/3126747

File

src/Context/EntityContextDefinition.php, line 21

Namespace

Drupal\rules\Context
View source
class EntityContextDefinition extends EntityContextDefinitionCore implements ContextDefinitionInterface {
    
    /**
     * The mapping of config export keys to internal properties.
     *
     * @var array
     */
    public 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
     */
    public $allowNull = FALSE;
    
    /**
     * The assignment restriction of this context.
     *
     * @var string|null
     *
     * @see \Drupal\rules\Context\ContextDefinitionInterface::getAssignmentRestriction()
     */
    public $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('EntityContextDefinition class must implement ' . ContextDefinitionInterface::class . '.');
        }
        // Default to Rules context definition class.
        $values['class'] = $values['class'] ?? EntityContextDefinition::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

Title Sort descending Modifiers Object type Summary Member alias Overriden Title Overrides
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::$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::addConstraint public function Adds a validation constraint. Overrides ContextDefinitionInterface::addConstraint
ContextDefinition::create public static function Creates a new context definition.
ContextDefinition::dataTypeMatches protected function Checks if this definition's data type matches that of the given context.
ContextDefinition::getConstraint public function Gets a validation constraint. Overrides ContextDefinitionInterface::getConstraint
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::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::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
ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_INPUT constant Constants for the context assignment restriction mode.
ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR constant
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function Aliased as: traitSleep 1
DependencySerializationTrait::__wakeup public function 2
EntityContextDefinition::$allowNull public property Whether the context value is allowed to be NULL or not.
EntityContextDefinition::$assignmentRestriction public property The assignment restriction of this context.
EntityContextDefinition::$nameMap public static property The mapping of config export keys to internal properties.
EntityContextDefinition::createFromArray public static function Creates a definition object from an exported array of values.
EntityContextDefinition::fromEntity public static function Creates a context definition from a given entity object.
EntityContextDefinition::fromEntityType public static function Creates a context definition from a given entity type.
EntityContextDefinition::fromEntityTypeId public static function Creates a context definition from a given entity type ID.
EntityContextDefinition::getAssignmentRestriction public function Determines if this context has an assignment restriction. Overrides ContextDefinitionInterface::getAssignmentRestriction
EntityContextDefinition::getConstraintObjects protected function Extracts an array of constraints for a context definition object. Overrides ContextDefinition::getConstraintObjects
EntityContextDefinition::getEntityTypeId protected function Returns the entity type ID of this context.
EntityContextDefinition::getSampleValues protected function Returns typed data objects representing this context definition. Overrides ContextDefinition::getSampleValues
EntityContextDefinition::isAllowedNull public function Determines if the context value is allowed to be NULL. Overrides ContextDefinitionInterface::isAllowedNull
EntityContextDefinition::setAllowNull public function Sets the "allow NULL value" behavior. Overrides ContextDefinitionInterface::setAllowNull
EntityContextDefinition::setAssignmentRestriction public function Sets the assignment restriction mode for this context. Overrides ContextDefinitionInterface::setAssignmentRestriction
EntityContextDefinition::toArray public function Exports the definition as an array. Overrides ContextDefinitionInterface::toArray
EntityContextDefinition::__construct public function Constructs a new context definition object. Overrides ContextDefinition::__construct
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