You are here

public function PreviewFactory::buildMarkup in View Modes Display 8.2

Build markup required to render the entity in the desired view mode.

@todo Handle block requirements better.

Parameters

Drupal\Core\Entity\ContentEntityInterface $entity: Entity class.

string $viewMode: Entity view mode.

Return value

array Render array for the given entity.

1 call to PreviewFactory::buildMarkup()
PreviewFactory::preview in src/Service/PreviewFactory.php
Preview entity view modes.

File

src/Service/PreviewFactory.php, line 157

Class

PreviewFactory
Class PreviewFactory.

Namespace

Drupal\view_modes_display\Service

Code

public function buildMarkup(ContentEntityInterface $entity, $viewMode) {
  $entityTypeId = $entity
    ->getEntityType()
    ->get('id');
  $viewBuilder = $this->entityTypeManager
    ->getViewBuilder($entityTypeId);
  if ('block_content' == $entityTypeId) {
    $blockManager = \Drupal::service('plugin.manager.block');
    $blockInstance = $blockManager
      ->createInstance('block_content:' . $entity
      ->uuid(), [
      'view_mode' => $viewMode,
    ]);
    return [
      // @todo Should be in BlockBase, wait https://www.drupal.org/node/2931040.
      '#theme' => 'block',
      '#configuration' => $blockInstance
        ->getConfiguration(),
      '#plugin_id' => $blockInstance
        ->getPluginId(),
      '#base_plugin_id' => $blockInstance
        ->getBaseId(),
      '#derivative_plugin_id' => $blockInstance
        ->getDerivativeId(),
      'content' => $blockInstance
        ->build(),
    ];
  }
  return $viewBuilder
    ->view($entity, $viewMode);
}