You are here

public function LinkExportFormatter::viewElements in REST Views 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/LinkExportFormatter.php \Drupal\rest_views\Plugin\Field\FieldFormatter\LinkExportFormatter::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 LinkFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/LinkExportFormatter.php, line 26

Class

LinkExportFormatter
Export a link.

Namespace

Drupal\rest_views\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) : array {
  $elements = parent::viewElements($items, $langcode);
  foreach ($elements as $delta => $element) {

    /** @var \Drupal\Core\Url $url */
    $url = $element['#url'];
    $data = [
      'url' => $url
        ->toString(),
      'text' => $element['#title'],
    ];
    $elements[$delta] = [
      '#type' => 'data',
      '#data' => SerializedData::create($data),
    ];
  }
  return $elements;
}