You are here

final class FieldContextHandler in Core Context 8

Exposes contexts stored on an entity field.

Hierarchy

Expanded class hierarchy of FieldContextHandler

1 file declares its use of FieldContextHandler
core_context.module in ./core_context.module

File

src/FieldContextHandler.php, line 15

Namespace

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

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

  /**
   * The entity field manager service.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  private $entityFieldManager;

  /**
   * FieldContextHandler constructor.
   *
   * @param \Drupal\ctools\ContextMapperInterface $context_mapper
   *   The context mapper service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager service.
   */
  public function __construct(ContextMapperInterface $context_mapper, EntityFieldManagerInterface $entity_field_manager) {
    $this->contextMapper = $context_mapper;
    $this->entityFieldManager = $entity_field_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getContexts(EntityInterface $entity) {
    assert($entity instanceof FieldableEntityInterface);
    $entity_type_id = $entity
      ->getEntityTypeId();
    $field_map = $this->entityFieldManager
      ->getFieldMapByFieldType('context');
    if (empty($field_map[$entity_type_id])) {
      return [];
    }
    $field_name = key($field_map[$entity_type_id]);
    $contexts = [];
    if ($entity
      ->hasField($field_name) === FALSE) {
      return $contexts;
    }
    $items = $entity
      ->get($field_name);
    if ($items
      ->isEmpty()) {
      return $contexts;
    }

    /** @var \Drupal\core_context\Plugin\Field\FieldType\ContextItem $item */
    foreach ($items as $item) {
      $contexts[$item->id] = $item
        ->getValue();
    }
    $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.
FieldContextHandler::$contextMapper private property The context mapper service.
FieldContextHandler::$entityFieldManager private property The entity field manager service.
FieldContextHandler::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
FieldContextHandler::getContexts public function Returns all contexts attached to an entity. Overrides EntityContextHandlerInterface::getContexts
FieldContextHandler::__construct public function FieldContextHandler constructor.