class EntityContextDefinition

Same name in other branches
  1. 9 core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php \Drupal\Core\Plugin\Context\EntityContextDefinition
  2. 10 core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php \Drupal\Core\Plugin\Context\EntityContextDefinition
  3. 11.x core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php \Drupal\Core\Plugin\Context\EntityContextDefinition

Defines a class to provide entity context definitions.

Hierarchy

Expanded class hierarchy of EntityContextDefinition

14 files declare their use of EntityContextDefinition
ContextHandlerTest.php in core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php
ContextPluginTest.php in core/tests/Drupal/KernelTests/Core/Plugin/ContextPluginTest.php
DefaultsSectionStorageTest.php in core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php
DefaultsSectionStorageTest.php in core/modules/layout_builder/tests/src/Kernel/DefaultsSectionStorageTest.php
Entity.php in core/modules/views/src/Plugin/views/argument_validator/Entity.php

... See full list

2 string references to 'EntityContextDefinition'
ContextDefinition::__sleep in core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
Implements magic __sleep() method.
EntityContextDefinitionDeprecationTest::setUp in core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionDeprecationTest.php

File

core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php, line 15

Namespace

Drupal\Core\Plugin\Context
View source
class EntityContextDefinition extends ContextDefinition {
    
    /**
     * {@inheritdoc}
     */
    public function __construct($data_type = 'any', $label = NULL, $required = TRUE, $multiple = FALSE, $description = NULL, $default_value = NULL) {
        // Prefix the data type with 'entity:' so that this class can be constructed
        // like so: new EntityContextDefinition('node')
        if (strpos($data_type, 'entity:') !== 0) {
            $data_type = "entity:{$data_type}";
        }
        parent::__construct($data_type, $label, $required, $multiple, $description, $default_value);
    }
    
    /**
     * Returns the entity type ID of this context.
     *
     * @return string
     *   The entity type ID.
     */
    protected function getEntityTypeId() {
        // The data type is the entity type ID prefixed by 'entity:' (7 characters).
        return substr($this->getDataType(), 7);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getConstraintObjects() {
        if (!$this->getConstraint('EntityType')) {
            $this->addConstraint('EntityType', [
                'type' => $this->getEntityTypeId(),
            ]);
        }
        return parent::getConstraintObjects();
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getSampleValues() {
        // Get the constraints from the context's definition.
        $constraints = $this->getConstraintObjects();
        $entity_type_manager = \Drupal::entityTypeManager();
        $entity_type_id = $this->getEntityTypeId();
        $entity_type = $entity_type_manager->getDefinition($entity_type_id);
        $storage = $entity_type_manager->getStorage($entity_type_id);
        // If the storage can generate a sample entity we might delegate to that.
        if ($storage instanceof ContentEntityStorageInterface) {
            if (!empty($constraints['Bundle']) && $constraints['Bundle'] instanceof BundleConstraint) {
                foreach ($constraints['Bundle']->getBundleOption() as $bundle) {
                    // We have a bundle, we are bundleable and we can generate a sample.
                    $values = [
                        $entity_type->getKey('bundle') => $bundle,
                    ];
                    (yield EntityAdapter::createFromEntity($storage->create($values)));
                }
                return;
            }
        }
        // Either no bundle, or not bundleable, so generate an entity adapter.
        $definition = EntityDataDefinition::create($entity_type_id);
        (yield new EntityAdapter($definition));
    }
    
    /**
     * Creates a context definition from a given entity type ID.
     *
     * @param string $entity_type_id
     *   The entity type ID from which to derive a context definition.
     *
     * @return static
     */
    public static function fromEntityTypeId($entity_type_id) {
        $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
        return static::fromEntityType($entity_type);
    }
    
    /**
     * Creates a context definition from a given entity type.
     *
     * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
     *   The entity type from which to derive a context definition.
     *
     * @return static
     */
    public static function fromEntityType(EntityTypeInterface $entity_type) {
        return new static('entity:' . $entity_type->id(), $entity_type->getLabel());
    }
    
    /**
     * Creates a context definition from a given entity object.
     *
     * @param \Drupal\Core\Entity\EntityInterface $entity
     *   The entity from which to derive a context definition.
     *
     * @return static
     */
    public static function fromEntity(EntityInterface $entity) {
        return static::fromEntityType($entity->getEntityType());
    }

}

Members

Title Sort descending Deprecated 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::$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::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::initializeEntityContextDefinition private function Initializes $this->entityContextDefinition for backwards compatibility.
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
ContextDefinition::__sleep public function Implements magic __sleep() method.
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
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::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::__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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.