|
<?php |
|
|
|
/** |
|
* Implements hook_preprocess_media__BUNDLE__VIEW_MODE() for remote_video, hero. |
|
*/ |
|
function MYSITETHEME_preprocess_media__remote_video(array &$variables) { |
|
/** @var \Drupal\media\MediaInterface $media */ |
|
$media = $variables['media']; |
|
|
|
// Looking for the fields that contain the media entity to the remote videos. |
|
if (isset($variables['media_embed'][0]['children']['#type'])) { |
|
$type = $variables['media_embed'][0]['children']['#type']; |
|
|
|
if ($type == 'video_embed_iframe') { |
|
// We want to identify the provider. |
|
$provider = $variables['media_embed'][0]['children']['#provider']; |
|
$url = $variables['media_embed'][0]['children']['#url']; |
|
$parsed_url = parse_url($url); |
|
|
|
$options = []; |
|
$options['query'] = isset($parsed_url['query']) ? parse_str($parsed_url['query']) : []; |
|
|
|
if ($provider == 'youtube') { |
|
$options['query']['rel'] = 0; |
|
$options['query']['autoplay'] = 0; |
|
$options['query']['enablejsapi'] = 1; // This is the critical value for stopping those videos. |
|
$options['query']['modestbranding'] = 1; |
|
} |
|
elseif ($provider == 'vimeo') { |
|
$options['query']['autoplay'] = 0; |
|
$options['query']['background'] = 1; |
|
$options['query']['loop'] = 1; |
|
} |
|
|
|
/** @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface $unrouted_url_assembler */ |
|
$unrouted_url_assembler = Drupal::service('unrouted_url_assembler'); |
|
$url = $unrouted_url_assembler->assemble($url, $options, FALSE); |
|
|
|
$variables['media_embed'][0]['children']['#url'] = $url; |
|
} |
|
} |
|
|
|
// Make video autoplay, loop and disabling any controls and branding. |
|
if (isset($variables['media_embed'][0]['#build']['settings']['scheme']) && $variables['media_embed'][0]['#build']['settings']['type'] === 'video') { |
|
$settings = $variables['media_embed'][0]['#build']['settings']; |
|
$settings['autoplay'] = TRUE; |
|
|
|
// Make modifications by provider. |
|
$url = !empty($settings['autoplay_url']) ? $settings['autoplay_url'] : $settings['embed_url']; |
|
|
|
if (UrlHelper::isExternal($url)) { |
|
$options = []; |
|
|
|
switch ($settings['scheme']) { |
|
case 'vimeo': |
|
$options['query']['autoplay'] = 1; |
|
$options['query']['background'] = 1; |
|
$options['query']['loop'] = 1; |
|
$options['query']['muted'] = 1; |
|
break; |
|
|
|
case 'youtube': |
|
$options['query']['autoplay'] = 1; |
|
$options['query']['controls'] = 0; |
|
$options['query']['disablekb'] = 1; |
|
$options['query']['fs'] = 0; |
|
$options['query']['mute'] = 1; |
|
$options['query']['loop'] = 1; |
|
$options['query']['enablejsapi'] = 1; |
|
$options['query']['modestbranding'] = 1; |
|
$options['query']['playlist'] = $settings['video_id']; |
|
break; |
|
} |
|
|
|
// Replace url. |
|
/** @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface $unrouted_url_assembler */ |
|
$unrouted_url_assembler = Drupal::service('unrouted_url_assembler'); |
|
$url = $unrouted_url_assembler->assemble($url, $options, FALSE); |
|
|
|
$settings['autoplay_url'] = $url; |
|
$settings['embed_url'] = $url; |
|
|
|
$variables['media_embed'][0]['#build']['settings'] = $settings; |
|
$variables['media_embed']['#blazy'] = $settings; |
|
} |
|
} |
|
} |