You are here

public function Html5::renderThumbnail in Video Embed HTML5 8

File

src/Plugin/video_embed_field/Provider/Html5.php, line 131

Class

Html5
A Html5 provider plugin.

Namespace

Drupal\video_embed_html5\Plugin\video_embed_field\Provider

Code

public function renderThumbnail($image_style, $link_url) {
  $build = parent::renderThumbnail($image_style, $link_url);
  if (!file_exists($this
    ->getLocalThumbnailUri()) && !$this->phpFFMpeg) {

    // Set uri to default placeholder.
    $uri = drupal_get_path('module', 'video_embed_html5') . '/img/placeholder.png';
    if ($this->config
      ->get('add_placeholder')) {
      if (($file = $this->config
        ->get('placeholder')) && ($file = File::load($file[0]))) {

        // Custom placeholder is uploaded, use this one.

        /** @var FileInterface $file */
        $uri = $file
          ->getFileUri();
      }
    }

    // Build render array for placeholder.
    $placeholder = [
      '#theme' => 'image',
      '#uri' => $uri,
    ];

    // Generate thumb in JS and render it as canvas.
    if ($link_url) {
      $build['#title'] = [
        '#type' => 'container',
        '#attributes' => [
          'data-render-thumbnail' => $this->videoUrl,
          'id' => 'video-embed-html5-' . uniqid(),
        ],
      ];
      if ($this->config
        ->get('add_placeholder')) {
        $build['#title']['image'] = $placeholder;
      }
    }
    else {
      $build = [
        '#type' => 'container',
        '#attributes' => [
          'data-render-thumbnail' => $this->videoUrl,
          'id' => 'video-embed-html5-' . uniqid(),
        ],
      ];
      if ($this->config
        ->get('add_placeholder')) {
        $build['image'] = $placeholder;
      }
    }

    // Attach lib to generate thumbnails.
    $build['#attached']['library'][] = 'video_embed_html5/thumbnails';
  }
  return $build;
}