You are here

UrlPlainFormatter.php in Drupal 8

File

core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php
View source
<?php

namespace Drupal\file\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\file\FileInterface;

/**
 * Plugin implementation of the 'file_url_plain' formatter.
 *
 * @FieldFormatter(
 *   id = "file_url_plain",
 *   label = @Translation("URL to file"),
 *   field_types = {
 *     "file"
 *   }
 * )
 */
class UrlPlainFormatter extends FileFormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($this
      ->getEntitiesToView($items, $langcode) as $delta => $file) {
      assert($file instanceof FileInterface);
      $elements[$delta] = [
        '#markup' => $file
          ->createFileUrl(),
        '#cache' => [
          'tags' => $file
            ->getCacheTags(),
        ],
      ];
    }
    return $elements;
  }

}

Classes

Namesort descending Description
UrlPlainFormatter Plugin implementation of the 'file_url_plain' formatter.