You are here

public function Twitter::thumbnail in Media entity Twitter 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/Twitter.php, line 390

Class

Twitter
Provides media type plugin for Twitter.

Namespace

Drupal\media_entity_twitter\Plugin\MediaEntity\Type

Code

public function thumbnail(MediaInterface $media) {

  // If there's already a local image, use it.
  if ($local_image = $this
    ->getField($media, 'image_local')) {
    return $local_image;
  }

  // If thumbnail generation is disabled, use the default thumbnail.
  if (empty($this->configuration['generate_thumbnails'])) {
    return $this
      ->getDefaultThumbnail();
  }

  // We might need to generate a thumbnail...
  $id = $this
    ->getField($media, 'id');
  $thumbnail_uri = $this
    ->getLocalImageUri($id);

  // ...unless we already have, in which case, use it.
  if (file_exists($thumbnail_uri)) {
    return $thumbnail_uri;
  }

  // Render the thumbnail SVG using the theme system.
  $thumbnail = [
    '#theme' => 'media_entity_twitter_tweet_thumbnail',
    '#content' => $this
      ->getField($media, 'content'),
    '#author' => $this
      ->getField($media, 'user'),
    '#avatar' => $this
      ->getField($media, 'profile_image_url_https'),
  ];
  $svg = $this->renderer
    ->renderRoot($thumbnail);
  return file_unmanaged_save_data($svg, $thumbnail_uri, FILE_EXISTS_ERROR) ?: $this
    ->getDefaultThumbnail();
}