You are here

final class SettingsContextHandler in Core Context 8

Exposes contexts stored in an entity's third-party settings.

Hierarchy

Expanded class hierarchy of SettingsContextHandler

2 files declare their use of SettingsContextHandler
core_context.module in ./core_context.module
core_context_test.module in tests/modules/core_context_test/core_context_test.module

File

src/SettingsContextHandler.php, line 14

Namespace

Drupal\core_context
View source
final class SettingsContextHandler implements EntityContextHandlerInterface {
  use CacheableContextTrait;

  /**
   * The context mapper service.
   *
   * @var \Drupal\ctools\ContextMapperInterface
   */
  private $contextMapper;

  /**
   * SettingsContextHandler constructor.
   *
   * @param \Drupal\ctools\ContextMapperInterface $context_mapper
   *   The context mapper service.
   */
  public function __construct(ContextMapperInterface $context_mapper) {
    $this->contextMapper = $context_mapper;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($container
      ->get('ctools.context_mapper'));
  }

  /**
   * {@inheritdoc}
   */
  public function getContexts(EntityInterface $entity) {
    assert($entity instanceof ThirdPartySettingsInterface);
    $contexts = $entity
      ->getThirdPartySetting('core_context', 'contexts', []);
    $contexts = $this->contextMapper
      ->getContextValues($contexts);
    return $this
      ->applyCaching($contexts, $entity);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableContextTrait::applyCaching private function Adds cache metadata to a set of contexts.
SettingsContextHandler::$contextMapper private property The context mapper service.
SettingsContextHandler::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
SettingsContextHandler::getContexts public function Returns all contexts attached to an entity. Overrides EntityContextHandlerInterface::getContexts
SettingsContextHandler::__construct public function SettingsContextHandler constructor.