public function RSSEnclosureFormatter::viewElements

Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().

Overrides FormatterInterface::viewElements

File

drupal/core/modules/file/lib/Drupal/file/Plugin/field/formatter/RSSEnclosureFormatter.php, line 32
Contains \Drupal\file\Plugin\field\formatter\RSSEnclosureFormatter.

Class

RSSEnclosureFormatter
Plugin implementation of the 'file_rss_enclosure' formatter.

Namespace

Drupal\file\Plugin\field\formatter

Code

public function viewElements(EntityInterface $entity, $langcode, array $items) {

  // Add the first file as an enclosure to the RSS item. RSS allows only one
  // enclosure per item. See: http://en.wikipedia.org/wiki/RSS_enclosure
  foreach ($items as $item) {
    if ($item['display']) {
      $entity->rss_elements[] = array(
        'key' => 'enclosure',
        'attributes' => array(
          'url' => file_create_url($item['uri']),
          'length' => $item['filesize'],
          'type' => $item['filemime'],
        ),
      );
      break;
    }
  }
}