You are here

public function FieldFormatterEntityEmbedDisplayBase::build in Entity Embed 8

Builds the renderable array for this Entity Embed display plugin.

Return value

array A renderable array representing the content of the embedded entity.

Overrides EntityEmbedDisplayBase::build

1 call to FieldFormatterEntityEmbedDisplayBase::build()
EntityReferenceFieldFormatter::build in src/Plugin/entity_embed/EntityEmbedDisplay/EntityReferenceFieldFormatter.php
Builds the renderable array for this Entity Embed display plugin.
1 method overrides FieldFormatterEntityEmbedDisplayBase::build()
EntityReferenceFieldFormatter::build in src/Plugin/entity_embed/EntityEmbedDisplay/EntityReferenceFieldFormatter.php
Builds the renderable array for this Entity Embed display plugin.

File

src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php, line 150

Class

FieldFormatterEntityEmbedDisplayBase
Base class for field formatter display plugins.

Namespace

Drupal\entity_embed\EntityEmbedDisplay

Code

public function build() {

  // Create a temporary entity to which our fake field value can be
  // added.
  $fakeEntity = EntityEmbedFakeEntity::create([
    'type' => '_entity_embed',
  ]);
  $definition = $this
    ->getFieldDefinition();

  /* @var \Drupal\Core\Field\FieldItemListInterface $items $items */

  // Create a field item list object, 1 is the value, array('target_id' => 1)
  // would work too, or multiple values. 1 is passed down from the list to the
  // field item, which knows that an integer is the ID.
  $items = $this->typedDataManager
    ->create($definition, $this
    ->getFieldValue($definition), $definition
    ->getName(), $fakeEntity
    ->getTypedData());

  // Prepare, expects an array of items, keyed by parent entity ID.
  $formatter = $this
    ->getFieldFormatter();
  $formatter
    ->prepareView([
    $fakeEntity
      ->id() => $items,
  ]);
  $build = $formatter
    ->viewElements($items, $this
    ->getLangcode());

  // For some reason $build[0]['#printed'] is TRUE, which means it will fail
  // to render later. So for now we manually fix that.
  // @todo Investigate why this is needed.
  show($build[0]);
  return $build[0];
}