You are here

public function YamlFormEntityReferenceLinkFormatter::viewElements in YAML Form 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/YamlFormEntityReferenceLinkFormatter.php, line 122

Class

YamlFormEntityReferenceLinkFormatter
Plugin implementation of the 'Link to form' formatter.

Namespace

Drupal\yamlform\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $source_entity = $items
    ->getEntity();
  $this->messageManager
    ->setSourceEntity($source_entity);
  $elements = [];
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {

    /** @var \Drupal\yamlform\YamlFormInterface $entity */
    if ($entity
      ->id() && $items[$delta]->status) {
      $link_options = [
        'query' => [
          'source_entity_type' => $source_entity
            ->getEntityTypeId(),
          'source_entity_id' => $source_entity
            ->id(),
        ],
      ];
      $elements[$delta] = [
        '#type' => 'link',
        '#title' => $this->token
          ->replace($this
          ->getSetting('label'), [
          'yamlform' => $entity,
        ]),
        '#url' => $entity
          ->toUrl('canonical', $link_options),
      ];
    }
    else {

      /** @var \Drupal\yamlform\YamlFormMessageManagerInterface $message_manager */
      $this->messageManager
        ->setYamlForm($entity);
      $elements[$delta] = $this->messageManager
        ->build(YamlFormMessageManagerInterface::FORM_CLOSED_MESSAGE);
    }
  }
  return $elements;
}