You are here

public function FeedsUriLinkFormatter::viewElements in Feeds 8.3

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 UriLinkFormatter::viewElements

File

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

Class

FeedsUriLinkFormatter
Plugin implementation of the 'feeds_uri_link' formatter.

Namespace

Drupal\feeds\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    if ($item
      ->isEmpty()) {
      continue;
    }
    $scheme = parse_url($item->value, PHP_URL_SCHEME);
    if ($scheme === 'http' || $scheme === 'https') {
      $elements[$delta] = [
        '#type' => 'link',
        '#url' => Url::fromUri($item->value),
        '#title' => $item->value,
      ];
    }
    else {
      $elements[$delta] = [
        '#markup' => Html::escape($item->value),
      ];
    }
  }
  return $elements;
}