[Drupal] Preventing cookies from embedded video

Hopkins-Lutz, Richard hopkins-lutz.1 at osu.edu
Thu Mar 18 07:58:16 EDT 2021


Hey Drupal folks.

Working with Ken Phillips on GDPR/Privacy compliance, I crafted some code for Drupal 8 for preventing cookies from Youtube and Vimeo, and figured I would share it.

This is for Drupal 8, using its core oEmbed system, and should be universal if you use those:

use Drupal\media\IFrameMarkup;
use Drupal\media\OEmbed\Provider;

/**
 * Implements hook_oembed_resource_url_alter().
 */
function mymodule_oembed_resource_url_alter(array &$parsed_url, Provider $provider)
{
  // Add Do Not Track flag to Vimeo oEmbed requests
  if ($provider->getName() === 'Vimeo') {
    $parsed_url['query']['dnt'] = true;
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function mymodule_preprocess_media_oembed_iframe(&$variables)
{
  // Pull iframe tag
  $iFrame = $variables['media']->__toString();
  // Switch domain to the youtube nocookie domain.
  if (stripos($iFrame, 'youtube.com') !== false){
    $newFrame = str_replace('youtube.com/', 'youtube-nocookie.com/', $iFrame);
    $variables['media'] = IFrameMarkup::create($newFrame);
  }
}
Hope this helps a few of you.

[The Ohio State University]
Richard Hopkins-Lutz
Web Services Manager
College of Engineering Engineering Technology Services
hopkins-lutz.1 at osu.edu<mailto:hopkins-lutz.1 at osu.edu> osu.edu<http://osu.edu/>
He/Him/His/Mr.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osu.edu/pipermail/drupal/attachments/20210318/5b855d31/attachment.html>


More information about the Drupal mailing list