class MultipleStaticContext in Drupal 9
Same name and namespace in other branches
- 8 core/modules/block/tests/modules/block_test/src/ContextProvider/MultipleStaticContext.php \Drupal\block_test\ContextProvider\MultipleStaticContext
- 10 core/modules/block/tests/modules/block_test/src/ContextProvider/MultipleStaticContext.php \Drupal\block_test\ContextProvider\MultipleStaticContext
Sets multiple contexts for a static value.
Hierarchy
- class \Drupal\block_test\ContextProvider\MultipleStaticContext implements ContextProviderInterface
Expanded class hierarchy of MultipleStaticContext
1 string reference to 'MultipleStaticContext'
- block_test.services.yml in core/
modules/ block/ tests/ modules/ block_test/ block_test.services.yml - core/modules/block/tests/modules/block_test/block_test.services.yml
1 service uses MultipleStaticContext
- block_test.multiple_static_context in core/
modules/ block/ tests/ modules/ block_test/ block_test.services.yml - Drupal\block_test\ContextProvider\MultipleStaticContext
File
- core/
modules/ block/ tests/ modules/ block_test/ src/ ContextProvider/ MultipleStaticContext.php, line 14
Namespace
Drupal\block_test\ContextProviderView source
class MultipleStaticContext implements ContextProviderInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* Constructs a new MultipleStaticContext.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(AccountInterface $account, EntityTypeManagerInterface $entity_type_manager) {
$this->account = $account;
$this->userStorage = $entity_type_manager
->getStorage('user');
}
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids) {
$current_user = $this->userStorage
->load($this->account
->id());
$context1 = EntityContext::fromEntity($current_user, 'User A');
$context2 = EntityContext::fromEntity($current_user, 'User B');
$cacheability = new CacheableMetadata();
$cacheability
->setCacheContexts([
'user',
]);
$context1
->addCacheableDependency($cacheability);
$context2
->addCacheableDependency($cacheability);
return [
'userA' => $context1,
'userB' => $context2,
];
}
/**
* {@inheritdoc}
*/
public function getAvailableContexts() {
return $this
->getRuntimeContexts([]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MultipleStaticContext:: |
protected | property | The current user. | |
MultipleStaticContext:: |
protected | property | The user storage. | |
MultipleStaticContext:: |
public | function |
Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface:: |
|
MultipleStaticContext:: |
public | function |
Gets runtime context values for the given context IDs. Overrides ContextProviderInterface:: |
|
MultipleStaticContext:: |
public | function | Constructs a new MultipleStaticContext. |