You are here

public function File::getMetadata in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/src/Plugin/media/Source/File.php \Drupal\media\Plugin\media\Source\File::getMetadata()

Gets the value for a metadata attribute for a given media item.

Parameters

\Drupal\media\MediaInterface $media: A media item.

string $attribute_name: Name of the attribute to fetch.

Return value

mixed|null Metadata attribute value or NULL if unavailable.

Overrides MediaSourceBase::getMetadata

1 call to File::getMetadata()
Image::getMetadata in core/modules/media/src/Plugin/media/Source/Image.php
Gets the value for a metadata attribute for a given media item.
1 method overrides File::getMetadata()
Image::getMetadata in core/modules/media/src/Plugin/media/Source/Image.php
Gets the value for a metadata attribute for a given media item.

File

core/modules/media/src/Plugin/media/Source/File.php, line 60

Class

File
File entity media source.

Namespace

Drupal\media\Plugin\media\Source

Code

public function getMetadata(MediaInterface $media, $attribute_name) {

  /** @var \Drupal\file\FileInterface $file */
  $file = $media
    ->get($this->configuration['source_field'])->entity;

  // If the source field is not required, it may be empty.
  if (!$file) {
    return parent::getMetadata($media, $attribute_name);
  }
  switch ($attribute_name) {
    case static::METADATA_ATTRIBUTE_NAME:
    case 'default_name':
      return $file
        ->getFilename();
    case static::METADATA_ATTRIBUTE_MIME:
      return $file
        ->getMimeType();
    case static::METADATA_ATTRIBUTE_SIZE:
      return $file
        ->getSize();
    case 'thumbnail_uri':
      return $this
        ->getThumbnail($file) ?: parent::getMetadata($media, $attribute_name);
    default:
      return parent::getMetadata($media, $attribute_name);
  }
}