You are here

public function AggregatorTitleFormatter::viewElements in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/aggregator/src/Plugin/Field/FieldFormatter/AggregatorTitleFormatter.php \Drupal\aggregator\Plugin\Field\FieldFormatter\AggregatorTitleFormatter::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

core/modules/aggregator/src/Plugin/Field/FieldFormatter/AggregatorTitleFormatter.php, line 53

Class

AggregatorTitleFormatter
Plugin implementation of the 'aggregator_title' formatter.

Namespace

Drupal\aggregator\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  if ($items
    ->getEntity()
    ->getEntityTypeId() == 'aggregator_feed') {
    $url_string = $items
      ->getEntity()
      ->getUrl();
  }
  else {
    $url_string = $items
      ->getEntity()
      ->getLink();
  }
  foreach ($items as $delta => $item) {
    if ($this
      ->getSetting('display_as_link') && $url_string) {
      $elements[$delta] = [
        '#type' => 'link',
        '#title' => $item->value,
        '#url' => Url::fromUri($url_string),
      ];
    }
    else {
      $elements[$delta] = [
        '#markup' => $item->value,
      ];
    }
  }
  return $elements;
}