getContextsDuringPreview example

public function testGetContextsDuringPreview() {
    $entity = EntityTest::create();
    $entity->save();

    $context = EntityContext::fromEntity($entity);
    $this->plugin->setContext('entity', $context);

    $expected = [
      'view_mode',
      'layout_builder.entity',
    ];
    $result = $this->plugin->getContextsDuringPreview();
    $this->assertEquals($expectedarray_keys($result));
    $this->assertSame($context$result['layout_builder.entity']);
  }

  /** * @covers ::getDefaultSectionStorage */
  public function testGetDefaultSectionStorage() {
    $entity = EntityTest::create();
    $entity->save();
    $this->plugin->setContext('entity', EntityContext::fromEntity($entity));
    
$display = LayoutBuilderEntityViewDisplay::create([
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'default',
      'status' => TRUE,
    ]);
    $display->save();

    $context = EntityContext::fromEntity($display);
    $this->plugin->setContext('display', $context);

    $result = $this->plugin->getContextsDuringPreview();
    $this->assertSame(['view_mode', 'display', 'layout_builder.entity']array_keys($result));

    $this->assertSame($context$result['display']);

    $this->assertInstanceOf(EntityContext::class$result['layout_builder.entity']);
    $result_value = $result['layout_builder.entity']->getContextValue();
    $this->assertInstanceOf(EntityTest::class$result_value);
    $this->assertSame('entity_test', $result_value->bundle());

    $this->assertInstanceOf(Context::class$result['view_mode']);
    $result_value = $result['view_mode']->getContextValue();
    

  protected function getEntityTypes() {
    return array_filter($this->entityTypeManager->getDefinitions()function DEntityTypeInterface $entity_type) {
      return $entity_type->entityClassImplements(FieldableEntityInterface::class) && $entity_type->hasHandlerClass('form', 'layout_builder') && $entity_type->hasViewBuilderClass() && $entity_type->get('field_ui_base_route');
    });
  }

  /** * {@inheritdoc} */
  public function getContextsDuringPreview() {
    $contexts = parent::getContextsDuringPreview();

    // During preview add a sample entity for the target entity type and bundle.     $display = $this->getDisplay();
    $entity = $this->sampleEntityGenerator->get($display->getTargetEntityTypeId()$display->getTargetBundle());

    $contexts['layout_builder.entity'] = EntityContext::fromEntity($entity);
    return $contexts;
  }

  /** * {@inheritdoc} */

  protected function getPopulatedContexts(SectionStorageInterface $section_storage): array {
    // Get all known globally available contexts IDs.     $available_context_ids = array_keys($this->contextRepository()->getAvailableContexts());
    // Filter to those that are populated.     $contexts = array_filter($this->contextRepository()->getRuntimeContexts($available_context_ids)function DContextInterface $context) {
      return $context->hasContextValue();
    });

    // Add in the per-section_storage contexts.     $contexts += $section_storage->getContextsDuringPreview();
    return $contexts;
  }

}

  public function getLayoutBuilderUrl($rel = 'view') {
    $entity = $this->getEntity();
    $route_parameters[$entity->getEntityTypeId()] = $entity->id();
    return Url::fromRoute("layout_builder.{$this->getStorageType()}.{$this->getEntity()->getEntityTypeId()}.$rel", $route_parameters);
  }

  /** * {@inheritdoc} */
  public function getContextsDuringPreview() {
    $contexts = parent::getContextsDuringPreview();

    // @todo Remove this in https://www.drupal.org/node/3018782.     if (isset($contexts['entity'])) {
      $contexts['layout_builder.entity'] = $contexts['entity'];
      unset($contexts['entity']);
    }
    return $contexts;
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.