You are here

public function MediaThumbnailManager::getFileObject in Media Thumbnails 8

Get a media file object, either source or thumbnail.

Parameters

\Drupal\media\MediaInterface $media: The given media entity.

string $field_name: The field name of the source file, or 'thumbnail'.

Return value

\Drupal\Core\Entity\EntityInterface|null The loaded file object.

2 calls to MediaThumbnailManager::getFileObject()
MediaThumbnailManager::getSource in src/Plugin/MediaThumbnailManager.php
Get the source file object for a media entity, if any.
MediaThumbnailManager::getThumbnail in src/Plugin/MediaThumbnailManager.php
Get the thumbnail file object for a media entity, if any.

File

src/Plugin/MediaThumbnailManager.php, line 201

Class

MediaThumbnailManager
Provides the Media thumbnail plugin manager.

Namespace

Drupal\media_thumbnails\Plugin

Code

public function getFileObject(MediaInterface $media, $field_name) {

  // Fetch the thumbnail file id, if any.
  try {
    $fid = $media
      ->get($field_name)
      ->first()
      ->getValue()['target_id'];
  } catch (Exception $e) {
    return NULL;
  }

  // Return the corresponding file object, if any.
  return $this->container
    ->get('entity_type.manager')
    ->getStorage('file')
    ->load($fid);
}