Skip to content

Instantly share code, notes, and snippets.

@pthurmond
Last active September 9, 2025 15:03
Show Gist options
  • Save pthurmond/a71dec661d8b62320037b3b34453b5f3 to your computer and use it in GitHub Desktop.
Save pthurmond/a71dec661d8b62320037b3b34453b5f3 to your computer and use it in GitHub Desktop.

Revisions

  1. pthurmond revised this gist Jan 7, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README_YouTube_Pause.md
    Original file line number Diff line number Diff line change
    @@ -9,3 +9,5 @@ Specifically, the "enablejsapi" parameter is what enables this. I will also shar
    To learn more about the optional parameters you can pass, look at this documentation: https://developers.google.com/youtube/player_parameters

    To learn more about the iframe API go here: https://developers.google.com/youtube/iframe_api_reference

    I also found this example of using the API including play, pause, and stop from Simon Ingeson: https://codepen.io/smonn/pen/gaeVae
  2. pthurmond revised this gist Jan 7, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion YouTubePauseDrupalHookExample.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    <?php


    /**
    * Implements hook_preprocess_media__BUNDLE__VIEW_MODE() for remote_video, hero.
    */
  3. pthurmond revised this gist Jan 7, 2022. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README_YouTube_Pause.md
    Original file line number Diff line number Diff line change
    @@ -5,3 +5,7 @@ This requires one more thing. The YouTube video URL needs parameters to tell it
    Example URL: https://www.youtube.com/embed/f5piHKZPoP4?rel=0&autoplay=0&enablejsapi=1

    Specifically, the "enablejsapi" parameter is what enables this. I will also share a php file with the Drupal hook I used for this.

    To learn more about the optional parameters you can pass, look at this documentation: https://developers.google.com/youtube/player_parameters

    To learn more about the iframe API go here: https://developers.google.com/youtube/iframe_api_reference
  4. pthurmond renamed this gist Jan 6, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. pthurmond renamed this gist Jan 6, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. pthurmond revised this gist Jan 6, 2022. No changes.
  7. pthurmond revised this gist Jan 6, 2022. No changes.
  8. pthurmond revised this gist Jan 6, 2022. 2 changed files with 95 additions and 0 deletions.
    88 changes: 88 additions & 0 deletions DrupalHookExample.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    <?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;
    }
    }
    }
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # Notes

    This requires one more thing. The YouTube video URL needs parameters to tell it to accept commands. In my application, which is Drupal 9, I use a hook to modify the URL of all YouTube video URLs.

    Example URL: https://www.youtube.com/embed/f5piHKZPoP4?rel=0&autoplay=0&enablejsapi=1

    Specifically, the "enablejsapi" parameter is what enables this. I will also share a php file with the Drupal hook I used for this.
  9. pthurmond created this gist Jan 6, 2022.
    15 changes: 15 additions & 0 deletions SwiperYoutube.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // This is how you pause a YouTube video on slide change.
    let $slider = new Swiper($sliderWrapper, {
    on: {
    slideChange: function (el) {
    $('.swiper-slide').each(function () {
    // Find the YouTube iframe.
    var youtubePlayer = $(this).find('iframe').get(0);

    if (youtubePlayer) {
    // Attempt to send a command to the player to stop the playing of the video.
    youtubePlayer.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
    }
    });
    }
    });