You are here

public function RSSEnclosureFormatter::viewElements in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\RSSEnclosureFormatter::viewElements()
  2. 10 core/modules/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\RSSEnclosureFormatter::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/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php, line 23

Class

RSSEnclosureFormatter
Plugin implementation of the 'file_rss_enclosure' formatter.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $entity = $items
    ->getEntity();

  // Add the first file as an enclosure to the RSS item. RSS allows only one
  // enclosure per item. See: http://wikipedia.org/wiki/RSS_enclosure
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $file) {

    /** @var \Drupal\file\FileInterface $file */
    $entity->rss_elements[] = [
      'key' => 'enclosure',
      'attributes' => [
        // In RSS feeds, it is necessary to use absolute URLs. The 'url.site'
        // cache context is already associated with RSS feed responses, so it
        // does not need to be specified here.
        'url' => $file
          ->createFileUrl(FALSE),
        'length' => $file
          ->getSize(),
        'type' => $file
          ->getMimeType(),
      ],
    ];
  }
  return [];
}