You are here

protected function YamlFormEntityReferenceTrait::formatLinks in YAML Form 8

Format an entity autocomplete as a link or a list of links.

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 as a link or a list of links.

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

File

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

Class

YamlFormEntityReferenceTrait
Provides an 'entity_reference' trait.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

protected function formatLinks(array $element, $value, array $options) {
  list($entity_ids, $entities) = $this
    ->getTargetEntities($element, $value, $options);
  $build = [];
  foreach ($entity_ids as $entity_id) {
    $entity = isset($entities[$entity_id]) ? $entities[$entity_id] : NULL;
    if ($entity) {
      $build[$entity_id] = [
        '#type' => 'link',
        '#title' => $entity
          ->label(),
        '#url' => $entity
          ->toUrl()
          ->setAbsolute(TRUE),
      ];
    }
    else {
      $build[$entity_id] = [
        '#markup' => $entity_id,
      ];
    }
  }
  if ($this
    ->isMultiline($element) || count($build) > 1) {
    return [
      '#theme' => 'item_list',
      '#items' => $build,
    ];
  }
  else {
    return reset($build);
  }
}