You are here

public function Library::getContextsDuringPreview in Layout builder library 8

Gets contexts for use during preview.

When not in preview, ::getContexts() will be used.

Return value

\Drupal\Core\Plugin\Context\ContextInterface[] The plugin contexts suitable for previewing.

Overrides SectionStorageBase::getContextsDuringPreview

File

src/Plugin/SectionStorage/Library.php, line 199

Class

Library
Defines a class for library based layout storage.

Namespace

Drupal\layout_library\Plugin\SectionStorage

Code

public function getContextsDuringPreview() {
  $contexts = parent::getContextsDuringPreview();
  $display = $this
    ->getLayout();
  $entity = $this->sampleEntityGenerator
    ->get($display
    ->getTargetEntityType(), $display
    ->getTargetBundle());
  $context_label = new TranslatableMarkup('@entity being viewed', [
    '@entity' => $entity
      ->getEntityType()
      ->getLabel(),
  ]);
  $contexts['layout_builder.entity'] = EntityContext::fromEntity($entity, $context_label);
  return $contexts;
}