You are here

protected function FileMediaFormatterBase::getSourceFiles in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase::getSourceFiles()
  2. 9 core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase::getSourceFiles()

Gets source files with attributes.

Parameters

\Drupal\Core\Field\EntityReferenceFieldItemListInterface $items: The item list.

string $langcode: The language code of the referenced entities to display.

Return value

array Numerically indexed array, which again contains an associative array with the following key/values:

1 call to FileMediaFormatterBase::getSourceFiles()
FileMediaFormatterBase::viewElements in core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php
Builds a renderable array for a field value.

File

core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php, line 189

Class

FileMediaFormatterBase
Base class for media file formatter.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

protected function getSourceFiles(EntityReferenceFieldItemListInterface $items, $langcode) {
  $source_files = [];

  // Because we can have the files grouped in a single media tag, we do a
  // grouping in case the multiple file behavior is not 'tags'.

  /** @var \Drupal\file\Entity\File $file */
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $file) {
    if (static::mimeTypeApplies($file
      ->getMimeType())) {
      $source_attributes = new Attribute();
      $source_attributes
        ->setAttribute('src', $file
        ->createFileUrl())
        ->setAttribute('type', $file
        ->getMimeType());
      if ($this
        ->getSetting('multiple_file_display_type') === 'tags') {
        $source_files[] = [
          [
            'file' => $file,
            'source_attributes' => $source_attributes,
          ],
        ];
      }
      else {
        $source_files[0][] = [
          'file' => $file,
          'source_attributes' => $source_attributes,
        ];
      }
    }
  }
  return $source_files;
}