In the Cookies Video module, Use thumbnail image as background

Created on 14 April 2022, about 2 years ago
Updated 8 November 2023, 8 months ago

Problem/Motivation

COOKIES consent management hides remote videos until user consent is given. It does not show the service provided thumbnail, since this would still allow the service provider to track the user.
However, if using core media Remote Video, a local version of this thumbnail is stored.
It would improve UX to optionally use this image as the background of the cookies-fallback--video div.

Steps to reproduce

Enable Core Media
Create a new Media type with "Remote Video" as "Media Source"
Enable COOKIES Video sub-module
Require consent to display remote videos
Add a Media field of the newly created remote video type to a content type (or paragraph)
Display said field as "rendered entity"

Visit a page with such a remote video field. The video display is blocked, with a blank background.

Proposed resolution

If a thumbnail image exists for the video, use it as the background image for the cookies-fallback--video div.

✨ Feature request
Status

Postponed

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France quimic Paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¨πŸ‡¦Canada bwaindwain

    Here's how we accomplished this for our project in a custom module. Maybe this will help someone or stimulate the brain cells.

    First, add the Drupal oembed thumbnail to the iframe.

    /**
     * Implements hook_preprocess_HOOK() for fields.
     */
    function YOURMODULE_preprocess_field(&$variables) {
      $formatter = $variables['element']['#formatter'];
      if ($formatter === 'oembed') {
        /** @var \Drupal\media\Entity\Media */
        $media = $variables['element']['#object'];
        /** @var \Drupal\file\Entity\File */
        $thumbnail = $media->get('thumbnail')->entity;
        /** @var \Drupal\Core\File\FileUrlGeneratorInterface */
        $file_url_generator = \Drupal::service('file_url_generator');
        foreach (Element::children($variables['items']) as $item) {
          if ($thumbnail_uri = $thumbnail->getFileUri()) {
            $thumbnail_path = $file_url_generator->generateString($thumbnail_uri);
            $variables['items'][$item]['content']['#attributes']['data-thumbnail'] = $thumbnail_path;
            $variables['items'][$item]['content']['#attached']['library'][] = 'YOURMODULE/remote_video';
          }
        }
      }
    

    Then, move the thumbnail image to the cookies overlay as a background-image with JS.

    /**
     * @file
     * Handles cookie consent events for remote videos.
     */
    (function (Drupal, $) {
      'use strict';
    
      var videoSelectors = 'iframe.media-oembed-content.cookies-video';
    
      Drupal.behaviors.YOURMODULECookiesRemoteVideo = {
    
        consentDenied: function (context) {
          $(videoSelectors, context).each(function (i, element) {
            if (element.dataset.thumbnail) {
              let wrapper = element.closest(".cookies-fallback--video--wrap")
              if (wrapper) {
                wrapper.style.backgroundImage = "url(" + element.dataset.thumbnail + ")";
              }
            }
          });
        },
    
        attach: function (context) {
          var self = this;
          document.addEventListener('cookiesjsrUserConsent', function (event) {
            var service = (typeof event.detail.services === 'object') ? event.detail.services : {};
            if (typeof service.video !== 'undefined' && !service.video) {
              self.consentDenied(context);
            }
          });
        }
      };
    
    })(Drupal, jQuery);
    
    

    Add a bit of CSS to blur and lighten the background-image.

    .cookies-fallback--video--wrap {
      background: none;
      background-size: cover;
      background-position: center;
      background-repeat: no-repeat;
    }
    .cookies-fallback--video--overlay {
      background: #ffffff99;
      backdrop-filter: blur(4px);
      border: none;
    }
    
  • πŸ‡©πŸ‡ͺGermany marcoka

    bwaindwain, very interesting code. I played around with that a bit but couldnt see hwo it works.
    The function "YOURMODULE_preprocess_field(&$variables) {" should render the thumbnail next to the video right?

  • πŸ‡¨πŸ‡¦Canada bwaindwain

    @marcoka The YOURMODULE_preprocess_field() adds the thumbnail URL as a data-thumbnail attribute on the <iframe>. Then the JS gets the image URL and applies as a background-image style to the parent <div>.

Production build 0.69.0 2024