class EntityLazyLoadContext

Same name and namespace in other branches
  1. 4.0.x src/Context/EntityLazyLoadContext.php \Drupal\ctools\Context\EntityLazyLoadContext

Defines context that loads entity on demand.

Hierarchy

Expanded class hierarchy of EntityLazyLoadContext

2 files declare their use of EntityLazyLoadContext
ContextMapper.php in src/ContextMapper.php
ContextMapperTest.php in tests/src/Unit/ContextMapperTest.php

File

src/Context/EntityLazyLoadContext.php, line 12

Namespace

Drupal\ctools\Context
View source
class EntityLazyLoadContext extends Context {
    
    /**
     * The entity UUID.
     *
     * @var string
     */
    protected $uuid;
    
    /**
     * The entity repository.
     *
     * @var \Drupal\Core\Entity\EntityRepositoryInterface
     */
    protected $entityRepository;
    
    /**
     * Construct an EntityLazyLoadContext object.
     *
     * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
     *   The context definition.
     * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
     *   The entity repository.
     * @param string $uuid
     *   The UUID of the entity.
     */
    public function __construct(ContextDefinitionInterface $context_definition, EntityRepositoryInterface $entity_repository, $uuid) {
        parent::__construct($context_definition);
        $this->entityRepository = $entity_repository;
        $this->uuid = $uuid;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContextValue() {
        if (!$this->contextData) {
            $entity_type_id = substr($this->contextDefinition
                ->getDataType(), 7);
            $this->setContextValue($this->entityRepository
                ->loadEntityByUuid($entity_type_id, $this->uuid));
        }
        return parent::getContextValue();
    }
    
    /**
     * {@inheritdoc}
     */
    public function hasContextValue() {
        // Ensure that the entity is loaded before checking if it exists.
        if (!$this->contextData) {
            $this->getContextValue();
        }
        return parent::hasContextValue();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Context::$cacheabilityMetadata protected property The cacheability metadata.
Context::$contextData protected property The data associated with the context.
Context::$contextDefinition protected property The definition to which a context must conform. Overrides Context::$contextDefinition
Context::$contextValue protected property The value of the context.
Context::addCacheableDependency public function Adds a dependency on an object: merges its cacheability metadata. Overrides ContextInterface::addCacheableDependency
Context::createFromContext public static function Creates a new context with a different value. Overrides ContextInterface::createFromContext
Context::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
Context::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
Context::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
Context::getConstraints public function Gets a list of validation constraints. Overrides Context::getConstraints
Context::getContextData public function Gets the context value as typed data object. Overrides ContextInterface::getContextData
Context::getContextDefinition public function Gets the provided definition that the context must conform to. Overrides Context::getContextDefinition
Context::setContextValue protected function Sets the context value.
Context::validate public function Validates the set context value. Overrides Context::validate
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityLazyLoadContext::$entityRepository protected property The entity repository.
EntityLazyLoadContext::$uuid protected property The entity UUID.
EntityLazyLoadContext::getContextValue public function Gets the context value. Overrides Context::getContextValue
EntityLazyLoadContext::hasContextValue public function Returns whether the context has a value. Overrides Context::hasContextValue
EntityLazyLoadContext::__construct public function Construct an EntityLazyLoadContext object. Overrides Context::__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