c4dacceb15e7bb78c55592ed299187bff537e2c0
[yaffs-website] / web / modules / contrib / ctools / src / Context / EntityLazyLoadContext.php
1 <?php
2
3 namespace Drupal\ctools\Context;
4
5 use Drupal\Core\Entity\EntityRepositoryInterface;
6 use Drupal\Core\Plugin\Context\Context;
7 use Drupal\Core\Plugin\Context\ContextDefinitionInterface;
8
9 /**
10  * @todo.
11  */
12 class EntityLazyLoadContext extends Context {
13
14   /**
15    * The entity UUID.
16    *
17    * @var string
18    */
19   protected $uuid;
20
21   /**
22    * The entity repository.
23    *
24    * @var \Drupal\Core\Entity\EntityRepositoryInterface
25    */
26   protected $entityRepository;
27
28   /**
29    * Construct an EntityLazyLoadContext object.
30    *
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.
35    * @param string $uuid
36    *   The UUID of the entity.
37    */
38   public function __construct(ContextDefinitionInterface $context_definition, EntityRepositoryInterface $entity_repository, $uuid) {
39     parent::__construct($context_definition);
40     $this->entityRepository = $entity_repository;
41     $this->uuid = $uuid;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
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));
51     }
52     return parent::getContextValue();
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function hasContextValue() {
59     // Ensure that the entity is loaded before checking if it exists.
60     if (!$this->contextData) {
61       $this->getContextValue();
62     }
63     return parent::hasContextValue();
64   }
65
66 }