3 namespace Drupal\block_test\ContextProvider;
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;
13 * Sets multiple contexts for a static value.
15 class MultipleStaticContext implements ContextProviderInterface {
20 * @var \Drupal\Core\Session\AccountInterface
27 * @var \Drupal\user\UserStorageInterface
29 protected $userStorage;
32 * Constructs a new MultipleStaticContext.
34 * @param \Drupal\Core\Session\AccountInterface $account
36 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
39 public function __construct(AccountInterface $account, EntityManagerInterface $entity_manager) {
40 $this->account = $account;
41 $this->userStorage = $entity_manager->getStorage('user');
47 public function getRuntimeContexts(array $unqualified_context_ids) {
48 $current_user = $this->userStorage->load($this->account->id());
50 $context1 = new Context(new ContextDefinition('entity:user', 'User A'), $current_user);
52 $context2 = new Context(new ContextDefinition('entity:user', 'User B'), $current_user);
54 $cacheability = new CacheableMetadata();
55 $cacheability->setCacheContexts(['user']);
57 $context1->addCacheableDependency($cacheability);
58 $context2->addCacheableDependency($cacheability);
69 public function getAvailableContexts() {
70 return $this->getRuntimeContexts([]);