dc89d5c64965d575b9b37b90156bb98254b7f143
[yaffs-website] / web / core / modules / block / tests / modules / block_test / src / ContextProvider / MultipleStaticContext.php
1 <?php
2
3 namespace Drupal\block_test\ContextProvider;
4
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Plugin\Context\ContextProviderInterface;
8 use Drupal\Core\Plugin\Context\EntityContext;
9 use Drupal\Core\Session\AccountInterface;
10
11 /**
12  * Sets multiple contexts for a static value.
13  */
14 class MultipleStaticContext implements ContextProviderInterface {
15
16   /**
17    * The current user.
18    *
19    * @var \Drupal\Core\Session\AccountInterface
20    */
21   protected $account;
22
23   /**
24    * The user storage.
25    *
26    * @var \Drupal\user\UserStorageInterface
27    */
28   protected $userStorage;
29
30   /**
31    * Constructs a new MultipleStaticContext.
32    *
33    * @param \Drupal\Core\Session\AccountInterface $account
34    *   The current user.
35    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
36    *   The entity manager.
37    */
38   public function __construct(AccountInterface $account, EntityManagerInterface $entity_manager) {
39     $this->account = $account;
40     $this->userStorage = $entity_manager->getStorage('user');
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getRuntimeContexts(array $unqualified_context_ids) {
47     $current_user = $this->userStorage->load($this->account->id());
48
49     $context1 = EntityContext::fromEntity($current_user, 'User A');
50     $context2 = EntityContext::fromEntity($current_user, 'User B');
51
52     $cacheability = new CacheableMetadata();
53     $cacheability->setCacheContexts(['user']);
54
55     $context1->addCacheableDependency($cacheability);
56     $context2->addCacheableDependency($cacheability);
57
58     return [
59       'userA' => $context1,
60       'userB' => $context2,
61     ];
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function getAvailableContexts() {
68     return $this->getRuntimeContexts([]);
69   }
70
71 }