You are here

abstract class ContextProviderBase in Core Context 8

Provides a base class for context providers.

Hierarchy

Expanded class hierarchy of ContextProviderBase

File

src/ContextProvider/ContextProviderBase.php, line 12

Namespace

Drupal\core_context\ContextProvider
View source
abstract class ContextProviderBase implements ContextProviderInterface {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * ContextProviderBase constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Extracts contexts from an entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity from which to extract contexts.
   *
   * @return \Drupal\Component\Plugin\Context\ContextInterface[]
   *   The contexts extracted from the entity.
   */
  protected function getContextsFromEntity(EntityInterface $entity) {
    $entity_type = $entity
      ->getEntityType();
    if ($entity_type
      ->hasHandlerClass('context')) {
      return $this->entityTypeManager
        ->getHandler($entity_type
        ->id(), 'context')
        ->getContexts($entity);
    }
    return [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextProviderBase::$entityTypeManager protected property The entity type manager service.
ContextProviderBase::getContextsFromEntity protected function Extracts contexts from an entity.
ContextProviderBase::__construct public function ContextProviderBase constructor. 1
ContextProviderInterface::getAvailableContexts public function Gets all available contexts for the purposes of configuration. 5
ContextProviderInterface::getRuntimeContexts public function Gets runtime context values for the given context IDs. 5