function AggregatorTitleFormatter::viewElements

Same name and namespace in other branches
  1. 8.9.x core/modules/aggregator/src/Plugin/Field/FieldFormatter/AggregatorTitleFormatter.php \Drupal\aggregator\Plugin\Field\FieldFormatter\AggregatorTitleFormatter::viewElements()

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.