diff --git a/src/Plugin/video_embed_field/Provider/Vimeo.php b/src/Plugin/video_embed_field/Provider/Vimeo.php index 70c7f6b..69f7eb7 100644 --- a/src/Plugin/video_embed_field/Provider/Vimeo.php +++ b/src/Plugin/video_embed_field/Provider/Vimeo.php @@ -14,6 +14,13 @@ use Drupal\video_embed_field\ProviderPluginBase; */ class Vimeo extends ProviderPluginBase { + /** + * The vimeo video url regular expression. + * + * @var string + */ + public static $vimeoUrlRex = "/^https?:\/\/(www\.)?vimeo.com\/(channels\/[a-zA-Z0-9]*\/)?(?[0-9]*)(?:\/(?[a-zA-Z0-9]+))?\/?(\#t=(\d+)s)?$/"; + /** * {@inheritdoc} */ @@ -35,6 +42,11 @@ class Vimeo extends ProviderPluginBase { if ($time_index = $this->getTimeIndex()) { $iframe['#fragment'] = sprintf('t=%s', $time_index); } + + // Check if the video has a hash parameter (h) + if ($hash = $this->getHashParameter()) { + $iframe['#query'] += ['h' => $hash]; + } return $iframe; } @@ -59,15 +71,15 @@ class Vimeo extends ProviderPluginBase { * {@inheritdoc} */ public static function getIdFromInput($input) { - preg_match('/^https?:\/\/(www\.)?vimeo.com\/(channels\/[a-zA-Z0-9]*\/)?(?[0-9]*)(\/[a-zA-Z0-9]+)?(\#t=(\d+)s)?$/', $input, $matches); + preg_match(self::$vimeoUrlRex, $input, $matches); return isset($matches['id']) ? $matches['id'] : FALSE; } /** * Get the time index from the URL. * - * @return string|FALSE - * A time index parameter to pass to the frame or FALSE if none is found. + * @return string|false + * A time index parameter to pass to the frame or false if none is found. */ protected function getTimeIndex() { preg_match('/\#t=(?(\d+)s)$/', $this->input, $matches); @@ -81,4 +93,16 @@ class Vimeo extends ProviderPluginBase { return $this->oEmbedData()->title; } + /** + * Get the hash parameter of the unlisted public vimeo video from user input. + * + * @return string|false + * The hash parameter to pass to the frame url has a query parameter + * or false if none is found. + */ + public function getHashParameter() { + preg_match(self::$vimeoUrlRex, $this->input, $matches); + return isset($matches['h']) ? $matches['h'] : FALSE; + } + } diff --git a/tests/src/Unit/ProviderUrlParseTest.php b/tests/src/Unit/ProviderUrlParseTest.php index 6f64b97..1a63d5c 100644 --- a/tests/src/Unit/ProviderUrlParseTest.php +++ b/tests/src/Unit/ProviderUrlParseTest.php @@ -348,4 +348,83 @@ class ProviderUrlParseTest extends UnitTestCase { ]; } + /** + * Test the Vimeo url hash paramter parsing. + * + * @dataProvider vimeoHashParameterTestCases + */ + public function testGetHashParameter($url, $expected) { + $provider = new Vimeo([ + 'input' => $url, + ], '', [], new MockHttpClient()); + + // Call the getHashParameter method. + $hashParameter = $provider->getHashParameter(); + + $this->assertEquals($expected, $hashParameter); + } + + /** + * A data provider for testGetHashParameter. + * + * @return array + * An array of test cases. + */ + public function vimeoHashParameterTestCases() { + return [ + 'vimeo url with hash' => [ + 'https://vimeo.com/123456789/abcd', + 'abcd', + ], + 'vimeo url without hash' => [ + 'https://vimeo.com/123456789', + FALSE, + ], + 'vimeo url time frame and without hash ' => [ + 'https://vimeo.com/123456789#t=150s', + FALSE, + ], + 'vimeo url with hash and time frame' => [ + 'https://vimeo.com/123456789/abcd#t=150s', + 'abcd', + ], + ]; + } + + /** + * Tests the vimeo renderEmbedCode method. + * + * @dataProvider renderVimeoEmbedCodeDataProvider + */ + public function testVimeoRenderEmbedCode($width, $height, $autoplay, $url, $expectedIframe) { + $provider = new Vimeo([ + 'input' => $url, + ], '', [], new MockHttpClient()); + + $actualIframe = $provider->renderEmbedCode($width, $height, $autoplay); + $this->assertEquals($expectedIframe, $actualIframe['#query']); + } + + /** + * Data provider for testRenderEmbedCode. + * + * @return array + * An array of test data. + */ + public function renderVimeoEmbedCodeDataProvider() { + return [ + 'vimeo url without hash' => [ + 640, 360, TRUE, 'https://vimeo.com/138627894', [ + 'autoplay' => TRUE, + ], + ], + 'vimeo url with hash' => [ + 640, 360, TRUE, 'https://vimeo.com/138627894/ja239', [ + 'autoplay' => TRUE, + 'h' => 'ja239', + ], + ], + ]; + } + }