3 namespace Drupal\block_test\ContextProvider;
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;
12 * Sets multiple contexts for a static value.
14 class MultipleStaticContext implements ContextProviderInterface {
19 * @var \Drupal\Core\Session\AccountInterface
26 * @var \Drupal\user\UserStorageInterface
28 protected $userStorage;
31 * Constructs a new MultipleStaticContext.
33 * @param \Drupal\Core\Session\AccountInterface $account
35 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
38 public function __construct(AccountInterface $account, EntityManagerInterface $entity_manager) {
39 $this->account = $account;
40 $this->userStorage = $entity_manager->getStorage('user');
46 public function getRuntimeContexts(array $unqualified_context_ids) {
47 $current_user = $this->userStorage->load($this->account->id());
49 $context1 = EntityContext::fromEntity($current_user, 'User A');
50 $context2 = EntityContext::fromEntity($current_user, 'User B');
52 $cacheability = new CacheableMetadata();
53 $cacheability->setCacheContexts(['user']);
55 $context1->addCacheableDependency($cacheability);
56 $context2->addCacheableDependency($cacheability);
67 public function getAvailableContexts() {
68 return $this->getRuntimeContexts([]);