3 namespace Drupal\ctools\Context;
5 use Drupal\Core\Entity\EntityRepositoryInterface;
6 use Drupal\Core\Plugin\Context\Context;
7 use Drupal\Core\Plugin\Context\ContextDefinitionInterface;
12 class EntityLazyLoadContext extends Context {
22 * The entity repository.
24 * @var \Drupal\Core\Entity\EntityRepositoryInterface
26 protected $entityRepository;
29 * Construct an EntityLazyLoadContext object.
31 * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
32 * The context definition.
33 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
34 * The entity repository.
36 * The UUID of the entity.
38 public function __construct(ContextDefinitionInterface $context_definition, EntityRepositoryInterface $entity_repository, $uuid) {
39 parent::__construct($context_definition);
40 $this->entityRepository = $entity_repository;
47 public function getContextValue() {
48 if (!$this->contextData) {
49 $entity_type_id = substr($this->contextDefinition->getDataType(), 7);
50 $this->setContextValue($this->entityRepository->loadEntityByUuid($entity_type_id, $this->uuid));
52 return parent::getContextValue();
58 public function hasContextValue() {
59 // Ensure that the entity is loaded before checking if it exists.
60 if (!$this->contextData) {
61 $this->getContextValue();
63 return parent::hasContextValue();