You are here

public function WebformEntityReferenceUrlFormatter::viewElements in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/Field/FieldFormatter/WebformEntityReferenceUrlFormatter.php \Drupal\webform\Plugin\Field\FieldFormatter\WebformEntityReferenceUrlFormatter::viewElements()

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/WebformEntityReferenceUrlFormatter.php, line 25

Class

WebformEntityReferenceUrlFormatter
Plugin implementation of the 'Webform url' formatter.

Namespace

Drupal\webform\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $source_entity = $items
    ->getEntity();
  $elements = [];

  /** @var \Drupal\webform\WebformInterface[] $entities */
  $entities = $this
    ->getEntitiesToView($items, $langcode);
  foreach ($entities as $delta => $entity) {
    $link_options = QueryStringWebformSourceEntity::getRouteOptionsQuery($source_entity);
    $link = [
      '#plain_text' => $entity
        ->toUrl('canonical', $link_options)
        ->toString(),
    ];
    $elements[$delta] = $link;
    $this
      ->setCacheContext($elements[$delta], $entity, $items[$delta]);
  }
  return $elements;
}