You are here

function _cookies_video_video_embed_field_handler in COOKiES Consent Management 1.0.x

Handling videos embed with "Video Embed Field (video_embed_field)" module.

@warning Video Embed Field module is unsupported. Consider using media oembed.

Parameters

array $variables: Field template variables (s. cookies_video_preprocess_field()).

1 call to _cookies_video_video_embed_field_handler()
cookies_video_preprocess_field in modules/cookies_video/cookies_video.module
Implements hook_preprocess_HOOK().

File

modules/cookies_video/cookies_video.module, line 116
Contains cookies_video.module.

Code

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';
      }
    }
  }
}