You are here

cookies_video.module in COOKiES Consent Management 1.0.x

File

modules/cookies_video/cookies_video.module
View source
<?php

/**
 * @file
 * Contains cookies_video.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\cookies\CookiesKnockOutService;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function cookies_video_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the cookies_ga module.
    case 'help.page.cookies_video':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Submodule of COOKiES to manage video media items (by "media" module) inside of COOKiES consent management.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function cookies_video_preprocess_field(&$variables) {
  $formatter = $variables["element"]["#formatter"];
  if (in_array($formatter, [
    'oembed',
    'blazy_oembed',
  ])) {
    _cookies_video_media_oembed_handler($variables);
  }
  if (in_array($formatter, [
    'video_embed_field_colorbox',
    'video_embed_field_lazyload',
    'video_embed_field_thumbnail',
    'video_embed_field_video',
    'video_embed_field_video_url',
  ])) {

    // Return early if not a handled formatter:
    _cookies_video_video_embed_field_handler($variables);
  }
}

/**
 * Handling oembed and blazy_oembed field formatters.
 *
 * @param array $variables
 *   Field template variables (s. cookies_video_preprocess_field()).
 */
function _cookies_video_media_oembed_handler(array &$variables) {
  $formatter = $variables["element"]["#formatter"];
  $doKo = CookiesKnockOutService::getInstance()
    ->doKnockOut();
  if ($doKo) {
    foreach ($variables["items"] as &$item) {
      switch ($formatter) {
        case 'oembed':
          _cookies_video_preprocess_field_item_oembed($item);
          break;
        case 'blazy_oembed':
          _cookies_video_preprocess_field_item_blazy_oembed($item);
          break;
        default:
      }

      // Attach library.
      if (!isset($item["content"]["#attached"])) {
        $item["content"]["#attached"] = [
          "library" => [],
        ];
      }
      if (!isset($item["content"]["#attached"]["library"])) {
        $item["content"]["#attached"]["library"] = [];
      }
      $item["content"]["#attached"]["library"][] = 'cookies_video/cookies_media';
    }
  }
}

/**
 * Handling field_item from oembed formatter.
 */
function _cookies_video_preprocess_field_item_oembed(&$item) {

  // Move src to data-src and replace src by fallback.
  $src = $item["content"]["#attributes"]["src"];
  $item["content"]["#attributes"]["data-src"] = $src;
  $item["content"]["#attributes"]["src"] = '';

  // Set marker class.
  if (!isset($item["content"]["#attributes"]["class"]) || !is_array($item["content"]["#attributes"]["class"])) {
    $item["content"]["#attributes"]["class"] = [];
  }
  $item["content"]["#attributes"]["class"][] = 'cookies-video';
}

/**
 * Handling field_item from blazy_oembed formatter.
 */
function _cookies_video_preprocess_field_item_blazy_oembed(&$item) {

  // Hide the blazy preview by default and set marker class.
  // This has to be done in ['#build'] for blazy to take effect:
  $item["content"]["#build"]['attributes']["class"][] = 'hidden';
  $item["content"]["#build"]['attributes']["class"][] = 'cookies-video-blazy-oembed';
}

/**
 * Handling videos embed with "Video Embed Field (video_embed_field)" module.
 *
 * @param array $variables
 *   Field template variables (s. cookies_video_preprocess_field()).
 *
 * @warning Video Embed Field module is unsupported. Consider using media oembed.
 */
function _cookies_video_video_embed_field_handler(array &$variables) {
  $formatter = $variables["element"]["#formatter"];
  if (!in_array($formatter, [
    'video_embed_field_colorbox',
    'video_embed_field_lazyload',
    'video_embed_field_thumbnail',
    'video_embed_field_video',
    'video_embed_field_video_url',
  ])) {

    // Return early if not a handled formatter:
    return;
  }
  $doKo = CookiesKnockOutService::getInstance()
    ->doKnockOut();
  if ($doKo) {
    foreach ($variables["items"] as &$item) {
      if (!empty($item['content']['children']['#type']) && $item['content']['children']['#type'] == 'video_embed_iframe' && !empty($item["content"]['children']["#url"])) {

        // We have to build the URL in attributes as video_embed_field
        // builds src in twig and we can't simply override
        // the twig file in our module. This workaround seems cleaner,
        // especially as video_embed_field isn't actively maintained anymore.
        $url = $item["content"]['children']["#url"];
        $query = !empty($item["content"]['children']["#query"]) ? $item["content"]['children']["#query"] : [];
        $fragment = $item["content"]['children']["#fragment"] ?? '';
        $src = Url::fromUri($url, [
          'query' => $query,
          'fragment' => $fragment,
          'absolute' => TRUE,
        ]);

        // Clear original URL to prevent iframe src loading.
        $item["content"]['children']["#url"] = NULL;

        // Set data-src to the original URL.
        $item["content"]['children']["#attributes"]["data-src"] = $src
          ->toString();

        // Set the original source to empty.
        $item["content"]['children']["#attributes"]["src"] = '';

        // Set marker class:
        $item["content"]['children']["#attributes"]["class"][] = 'cookies-video-embed-field';

        // Attach library.
        $item["content"]['children']["#attached"]["library"][] = 'cookies_video/cookies_video_embed_field';
      }
    }
  }
}

Functions

Namesort descending Description
cookies_video_help Implements hook_help().
cookies_video_preprocess_field Implements hook_preprocess_HOOK().
_cookies_video_media_oembed_handler Handling oembed and blazy_oembed field formatters.
_cookies_video_preprocess_field_item_blazy_oembed Handling field_item from blazy_oembed formatter.
_cookies_video_preprocess_field_item_oembed Handling field_item from oembed formatter.
_cookies_video_video_embed_field_handler Handling videos embed with "Video Embed Field (video_embed_field)" module.