From 3105e942786bd6a4db43845789d6ec305e4e2125 Mon Sep 17 00:00:00 2001
From: Gordon Heydon <gordon@heydon.com.au>
Date: Sat, 2 Nov 2019 15:27:39 +1100
Subject: [PATCH] Issue #3091986: Add ::buildEmbedUrl() so that the external
 processes can get it such as GraphQL

---
 .../video_embed_field/Provider/Vimeo.php      |  9 ++++++-
 .../video_embed_field/Provider/YouTube.php    |  9 ++++++-
 .../Provider/YouTubePlaylist.php              |  7 +++++
 src/ProviderPluginBase.php                    | 27 +++++++++++++++++++
 src/ProviderPluginInterface.php               |  8 ++++++
 5 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/src/Plugin/video_embed_field/Provider/Vimeo.php b/src/Plugin/video_embed_field/Provider/Vimeo.php
index 70c7f6b..de3c3c2 100644
--- a/src/Plugin/video_embed_field/Provider/Vimeo.php
+++ b/src/Plugin/video_embed_field/Provider/Vimeo.php
@@ -21,7 +21,7 @@ class Vimeo extends ProviderPluginBase {
     $iframe = [
       '#type' => 'video_embed_iframe',
       '#provider' => 'vimeo',
-      '#url' => sprintf('https://player.vimeo.com/video/%s', $this->getVideoId()),
+      '#url' => $this->getEmbedUrl(),
       '#query' => [
         'autoplay' => $autoplay,
       ],
@@ -81,4 +81,11 @@ class Vimeo extends ProviderPluginBase {
     return $this->oEmbedData()->title;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function buildEmbedUrl($value) {
+    return sprintf('https://player.vimeo.com/video/%s', $this->getVideoId());
+  }
+
 }
diff --git a/src/Plugin/video_embed_field/Provider/YouTube.php b/src/Plugin/video_embed_field/Provider/YouTube.php
index 224edd2..4e4b371 100644
--- a/src/Plugin/video_embed_field/Provider/YouTube.php
+++ b/src/Plugin/video_embed_field/Provider/YouTube.php
@@ -21,7 +21,7 @@ class YouTube extends ProviderPluginBase {
     $embed_code = [
       '#type' => 'video_embed_iframe',
       '#provider' => 'youtube',
-      '#url' => sprintf('https://www.youtube.com/embed/%s', $this->getVideoId()),
+      '#url' => $this->getEmbedUrl(),
       '#query' => [
         'autoplay' => $autoplay,
         'start' => $this->getTimeIndex(),
@@ -91,4 +91,11 @@ class YouTube extends ProviderPluginBase {
     return isset($matches['id']) ? $matches['id'] : FALSE;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function buildEmbedUrl($value) {
+    return sprintf('https://www.youtube.com/embed/%s', $this->getVideoId());
+  }
+
 }
diff --git a/src/Plugin/video_embed_field/Provider/YouTubePlaylist.php b/src/Plugin/video_embed_field/Provider/YouTubePlaylist.php
index 1043c5a..ecdf709 100644
--- a/src/Plugin/video_embed_field/Provider/YouTubePlaylist.php
+++ b/src/Plugin/video_embed_field/Provider/YouTubePlaylist.php
@@ -64,4 +64,11 @@ class YouTubePlaylist extends ProviderPluginBase {
     return isset($matches[$component]) ? $matches[$component] : FALSE;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function buildEmbedUrl($value) {
+    return sprintf('https://www.youtube.com/embed/videoseries?list=%s', $this->getVideoId());
+  }
+
 }
diff --git a/src/ProviderPluginBase.php b/src/ProviderPluginBase.php
index c3c4b0d..dfbae84 100644
--- a/src/ProviderPluginBase.php
+++ b/src/ProviderPluginBase.php
@@ -27,6 +27,13 @@ abstract class ProviderPluginBase extends PluginBase implements ProviderPluginIn
    */
   protected $videoId;
 
+  /**
+   * The embed url to use for this video.
+   *
+   * @var string
+   */
+  protected $embedUrl;
+
   /**
    * The input that caused the embed provider to be selected.
    *
@@ -68,6 +75,7 @@ abstract class ProviderPluginBase extends PluginBase implements ProviderPluginIn
     $this->input = $configuration['input'];
     $this->videoId = $this->getIdFromInput($configuration['input']);
     $this->httpClient = $http_client;
+    $this->embedUrl = $this->buildEmbedUrl($configuration['input']);
   }
 
   /**
@@ -80,6 +88,16 @@ abstract class ProviderPluginBase extends PluginBase implements ProviderPluginIn
     return $this->videoId;
   }
 
+  /**
+   * Build the embed URL.
+   *
+   * @param $value
+   *   the embed URL.
+   */
+  protected function buildEmbedUrl($value) {
+    return $value;
+  }
+
   /**
    * Get the file system service.
    *
@@ -172,4 +190,13 @@ abstract class ProviderPluginBase extends PluginBase implements ProviderPluginIn
     return $this->t('@provider Video (@id)', ['@provider' => $this->getPluginDefinition()['title'], '@id' => $this->getVideoId()]);
   }
 
+  /**
+   * Return the embedded url.
+   *
+   * @return string
+   */
+  public function getEmbedUrl() {
+    return $this->embedUrl;
+  }
+
 }
diff --git a/src/ProviderPluginInterface.php b/src/ProviderPluginInterface.php
index 863feeb..fc7ad29 100644
--- a/src/ProviderPluginInterface.php
+++ b/src/ProviderPluginInterface.php
@@ -97,4 +97,12 @@ interface ProviderPluginInterface extends PluginInspectionInterface {
    */
   public function getName();
 
+  /**
+   * Return the embedded url.
+   *
+   * @return string
+   *   Built URL for embedding video.
+   */
+  public function getEmbedUrl();
+
 }
-- 
2.23.0

