Skip to content

Instantly share code, notes, and snippets.

@wesruv
Created October 21, 2025 20:30
Show Gist options
  • Save wesruv/9897456ebf1f646fb85dd9b58e388548 to your computer and use it in GitHub Desktop.
Save wesruv/9897456ebf1f646fb85dd9b58e388548 to your computer and use it in GitHub Desktop.
Drupal video_embed_field private Vimeo video fix, applied to version 2.5
diff --git a/web/modules/contrib/video_embed_field/src/Plugin/video_embed_field/Provider/Vimeo.php b/web/modules/contrib/video_embed_field/src/Plugin/video_embed_field/Provider/Vimeo.php
index 70c7f6beb..3a7884cc5 100644
--- a/web/modules/contrib/video_embed_field/src/Plugin/video_embed_field/Provider/Vimeo.php
+++ b/web/modules/contrib/video_embed_field/src/Plugin/video_embed_field/Provider/Vimeo.php
@@ -14,6 +14,13 @@
*/
class Vimeo extends ProviderPluginBase {
+ /**
+ * The vimeo video url regular expression.
+ *
+ * @var string
+ */
+ public static $vimeoUrlRex = "/^https?:\/\/(www\.)?vimeo.com\/(channels\/[a-zA-Z0-9]*\/)?(?<id>[0-9]*)(?:\/(?<h>[a-zA-Z0-9]+))?\/?(\#t=(\d+)s)?$/";
+
/**
* {@inheritdoc}
*/
@@ -35,6 +42,12 @@ public function renderEmbedCode($width, $height, $autoplay) {
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,7 +72,7 @@ protected function oEmbedData() {
* {@inheritdoc}
*/
public static function getIdFromInput($input) {
- preg_match('/^https?:\/\/(www\.)?vimeo.com\/(channels\/[a-zA-Z0-9]*\/)?(?<id>[0-9]*)(\/[a-zA-Z0-9]+)?(\#t=(\d+)s)?$/', $input, $matches);
+ preg_match(self::$vimeoUrlRex, $input, $matches);
return isset($matches['id']) ? $matches['id'] : FALSE;
}
@@ -81,4 +94,16 @@ public function getName() {
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/web/modules/contrib/video_embed_field/tests/src/Unit/ProviderUrlParseTest.php b/web/modules/contrib/video_embed_field/tests/src/Unit/ProviderUrlParseTest.php
index 6f64b970c..1a63d5c15 100644
--- a/web/modules/contrib/video_embed_field/tests/src/Unit/ProviderUrlParseTest.php
+++ b/web/modules/contrib/video_embed_field/tests/src/Unit/ProviderUrlParseTest.php
@@ -348,4 +348,83 @@ public function vimeoTimeIndexTestCases() {
];
}
+ /**
+ * 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',
+ ],
+ ],
+ ];
+ }
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment