You are here

protected function YamlFormEntityReferenceTrait::formatView in YAML Form 8

Format an entity autocomplete targets using a view mode.

Parameters

array $element: An element.

array|mixed $value: A value.

array $options: An array of options.

Return value

array|string A render array containing an entity autocomplete targets using a view mode.

1 call to YamlFormEntityReferenceTrait::formatView()
YamlFormEntityReferenceTrait::formatHtml in src/Plugin/YamlFormElement/YamlFormEntityReferenceTrait.php

File

src/Plugin/YamlFormElement/YamlFormEntityReferenceTrait.php, line 335

Class

YamlFormEntityReferenceTrait
Provides an 'entity_reference' trait.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

protected function formatView(array $element, $value, array $options) {
  list($entity_ids, $entities) = $this
    ->getTargetEntities($element, $value, $options);
  $view_mode = $this
    ->getFormat($element);
  $build = [];
  foreach ($entity_ids as $entity_id) {
    $entity = isset($entities[$entity_id]) ? $entities[$entity_id] : NULL;
    $build[$entity_id] = $entity ? \Drupal::entityTypeManager()
      ->getViewBuilder($entity
      ->getEntityTypeId())
      ->view($entity, $view_mode) : [
      '#markup' => $entity_id,
    ];
  }
  if ($this
    ->isMultiline($element) || count($build) > 1) {
    return $build;
  }
  else {
    return reset($build);
  }
}