You are here

private function SectionComponentRenderArray::getContextsFromSectionStorage in Core Context 8

Extracts contexts from a section storage plugin.

Parameters

\Drupal\layout_builder\SectionStorageInterface $section_storage: The section storage plugin from which to extract contexts.

Return value

\Drupal\Component\Plugin\Context\ContextInterface[] The contexts extracted from the section storage.

1 call to SectionComponentRenderArray::getContextsFromSectionStorage()
SectionComponentRenderArray::setComponentContexts in src/EventSubscriber/SectionComponentRenderArray.php
Sets context values on a section component at render time.

File

src/EventSubscriber/SectionComponentRenderArray.php, line 112

Class

SectionComponentRenderArray
Reacts to a render array being generated for a layout section component.

Namespace

Drupal\core_context\EventSubscriber

Code

private function getContextsFromSectionStorage(SectionStorageInterface $section_storage) {

  // Since we need to get the section list by prying open the section storage,
  // we can only work with instances of SectionStorageBase, since they have a
  // protected getSectionList() method. This isn't very clean, but I spoke to
  // Tim Plunkett and he literally told me to do it this way.
  if ($section_storage instanceof SectionStorageBase) {
    $method = new \ReflectionMethod($section_storage, 'getSectionList');
    $method
      ->setAccessible(TRUE);

    /** @var \Drupal\layout_builder\SectionListInterface $section_list */
    $section_list = $method
      ->invoke($section_storage);
  }
  else {
    return [];
  }

  // If the section list is an entity field, we need to get the whole entity
  // since that's what we can extract contexts from.
  if ($section_list instanceof FieldItemListInterface) {
    $section_list = $section_list
      ->getEntity();
  }

  // If the section list still isn't an entity, then we don't have a way to
  // extract contexts from it.
  if (!$section_list instanceof EntityInterface) {
    return [];
  }

  // If the entity doesn't have a context handler, then we cannot get contexts
  // from it and there is nothing else to do.
  if (!$section_list
    ->getEntityType()
    ->hasHandlerClass('context')) {
    return [];
  }

  /** @var \Drupal\Component\Plugin\Context\ContextInterface[] $contexts */
  return $this->entityTypeManager
    ->getHandler($section_list
    ->getEntityTypeId(), 'context')
    ->getContexts($section_list);
}