You are here

public function Document::thumbnail in Media entity document 8

Gets thumbnail image.

Media type plugin is responsible for returning URI of the generic thumbnail if no other is available. This functions should always return a valid URI.

Parameters

MediaInterface $media: Media.

Return value

string URI of the thumbnail.

Overrides MediaTypeInterface::thumbnail

File

src/Plugin/MediaEntity/Type/Document.php, line 59

Class

Document
Provides media type plugin for Document.

Namespace

Drupal\media_entity_document\Plugin\MediaEntity\Type

Code

public function thumbnail(MediaInterface $media) {
  $source_field = $this->configuration['source_field'];

  /** @var \Drupal\file\FileInterface $file */
  $file = $media->{$source_field}->entity;
  if ($file) {
    $mimetype = $file
      ->getMimeType();
    $mimetype = explode('/', $mimetype);
    $thumbnail = $this->config
      ->get('icon_base') . "/{$mimetype[0]}-{$mimetype[1]}.png";
    if (!is_file($thumbnail)) {
      $thumbnail = $this->config
        ->get('icon_base') . "/{$mimetype[1]}.png";
      if (!is_file($thumbnail)) {
        $thumbnail = $this->config
          ->get('icon_base') . '/document.png';
      }
    }
  }
  else {
    $thumbnail = $this->config
      ->get('icon_base') . '/document.png';
  }
  return $thumbnail;
}