You are here

public function BlazyVideoTrait::buildVideo in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/Dejavu/BlazyVideoTrait.php \Drupal\blazy\Dejavu\BlazyVideoTrait::buildVideo()

Builds relevant video embed field settings based on the given media url.

Parameters

array $settings: An array of settings to be passed into theme_blazy().

string $external_url: A deprecated video URL for $settings['input_url'].

3 calls to BlazyVideoTrait::buildVideo()
BlazyVideoFormatter::buildElements in src/Plugin/Field/FieldFormatter/BlazyVideoFormatter.php
Build the blazy elements.
BlazyVideoFormatter::viewElements in src/Plugin/Field/FieldFormatter/BlazyVideoFormatter.php
Builds a renderable array for a field value.
BlazyVideoTrait::getMediaItem in src/Dejavu/BlazyVideoTrait.php
Gets the Media item thumbnail, or re-associate the file entity to ME.

File

src/Dejavu/BlazyVideoTrait.php, line 52

Class

BlazyVideoTrait
A Trait common for optional Media Entity and Video Embed Media integration.

Namespace

Drupal\blazy\Dejavu

Code

public function buildVideo(array &$settings = [], $external_url = '') {
  $settings['input_url'] = empty($settings['input_url']) ? $external_url : $settings['input_url'];

  /** @var \Drupal\video_embed_field\ProviderManagerInterface $video */
  if (!($video = self::videoEmbedMediaManager())) {
    return;
  }
  if (!($provider = $video
    ->loadProviderFromInput($settings['input_url']))) {
    return;
  }

  // Ensures thumbnail is available.
  $provider
    ->downloadThumbnail();

  // @todo extract URL from the SRC of final rendered TWIG instead.
  $render = $provider
    ->renderEmbedCode(640, 360, '0');
  $old_url = isset($render['#attributes']) && isset($render['#attributes']['src']) ? $render['#attributes']['src'] : '';
  $embed_url = isset($render['#url']) ? $render['#url'] : $old_url;
  $query = isset($render['#query']) ? $render['#query'] : [];

  // Prevents complication with multiple videos by now.
  unset($query['autoplay'], $query['auto_play']);
  $settings['video_id'] = $provider::getIdFromInput($settings['input_url']);
  $settings['embed_url'] = Url::fromUri($embed_url, [
    'query' => $query,
  ])
    ->toString();
  $settings['scheme'] = $video
    ->loadDefinitionFromInput($settings['input_url'])['id'];
  $settings['uri'] = $provider
    ->getLocalThumbnailUri();
  $settings['type'] = 'video';

  // Adds autoplay for media URL on lightboxes, saving another click.
  $url = $settings['embed_url'];
  if (strpos($url, 'autoplay') === FALSE || strpos($url, 'autoplay=0') !== FALSE) {
    $settings['autoplay_url'] = strpos($url, '?') === FALSE ? $url . '?autoplay=1' : $url . '&autoplay=1';
  }

  // Only applies when Image style is empty, no file API, no $item,
  // with unmanaged VEF image without image_style.
  // Prevents 404 warning when video thumbnail missing for a reason.
  if (empty($settings['image_style'])) {
    if ($data = @getimagesize($settings['uri'])) {
      list($settings['width'], $settings['height']) = $data;
    }
  }
}