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