You are here

class MultipleStaticContext in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/block/tests/modules/block_test/src/ContextProvider/MultipleStaticContext.php \Drupal\block_test\ContextProvider\MultipleStaticContext

Sets multiple contexts for a static value.

Hierarchy

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 20
Contains \Drupal\block_test\ContextProvider\MultipleStaticContext.

Namespace

Drupal\block_test\ContextProvider
View 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\EntityManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(AccountInterface $account, EntityManagerInterface $entity_manager) {
    $this->account = $account;
    $this->userStorage = $entity_manager
      ->getStorage('user');
  }

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $current_user = $this->userStorage
      ->load($this->account
      ->id());
    $context1 = new Context(new ContextDefinition('entity:user', 'User 1'), $current_user);
    $context2 = new Context(new ContextDefinition('entity:user', 'User 2'), $current_user);
    $cacheability = new CacheableMetadata();
    $cacheability
      ->setCacheContexts([
      'user',
    ]);
    $context1
      ->addCacheableDependency($cacheability);
    $context2
      ->addCacheableDependency($cacheability);
    return [
      'user1' => $context1,
      'user2' => $context2,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    return $this
      ->getRuntimeContexts([]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MultipleStaticContext::$account protected property The current user.
MultipleStaticContext::$userStorage protected property The user storage.
MultipleStaticContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
MultipleStaticContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
MultipleStaticContext::__construct public function Constructs a new MultipleStaticContext.