function File::getMetadata

Same name in other branches
  1. 9 core/modules/media/src/Plugin/media/Source/File.php \Drupal\media\Plugin\media\Source\File::getMetadata()
  2. 8.9.x core/modules/media/src/Plugin/media/Source/File.php \Drupal\media\Plugin\media\Source\File::getMetadata()
  3. 10 core/modules/media/src/Plugin/media/Source/File.php \Drupal\media\Plugin\media\Source\File::getMetadata()

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);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.