From f13a7ae350c528b56683987c3205ee656f6e58ea Mon Sep 17 00:00:00 2001 From: Benjamin Toussaint Date: Tue, 26 Mar 2024 14:29:37 +0100 Subject: [PATCH] Support YouTube Shorts - Remove "/shorts/" from path to get video ID - Invert default height and width to keep vertical aspect ratio --- CHANGELOG.md | 6 +++++- src/Service/UrlParser.php | 3 +++ src/VideoEmbedder.php | 8 ++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a5b8e4..f026153 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.4.0] - 2024-03-25 +### Added +- Support YouTube Shorts + ## [1.3.1] - 2024-03-19 ### Fixed - Fix Drupal deprecations @@ -12,7 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [1.2.5] - 2021-08-30 ### Added -- Add issue & pull request templates +- Add issue & pull request templates - Add coding standard fixers & gitignore file - Add README & CHANGELOG diff --git a/src/Service/UrlParser.php b/src/Service/UrlParser.php index 19a830b..13cfd84 100755 --- a/src/Service/UrlParser.php +++ b/src/Service/UrlParser.php @@ -40,6 +40,9 @@ public function parse($url): array $vid = trim($url['path'], '/'); } elseif (isset($url['path']) && strpos($url['path'], '/embed') !== false) { $vid = str_replace('/embed/', '', $url['path']); + } elseif (isset($url['path']) && strpos($url['path'], '/shorts') !== false) { + $vid = str_replace('/shorts/', '', $url['path']); + $type = VideoEmbedder::WM_EMBED_TYPE_YOUTUBE_SHORT; } elseif (isset($url['query']['v'])) { $vid = $url['query']['v']; } diff --git a/src/VideoEmbedder.php b/src/VideoEmbedder.php index 51802ac..9f5ee04 100755 --- a/src/VideoEmbedder.php +++ b/src/VideoEmbedder.php @@ -5,9 +5,10 @@ class VideoEmbedder { public const WM_EMBED_TYPE_YOUTUBE = 'youtube'; + public const WM_EMBED_TYPE_YOUTUBE_SHORT = 'youtube_short'; public const WM_EMBED_TYPE_VIMEO = 'vimeo'; - public static function create($url, $autoplay = false, $width = 640, $height = 360): ?array + public static function create($url, $autoplay = false, ?int $width = null, ?int $height = null): ?array { [$type, $vid] = \Drupal::service('wmvideo.url_parser')->parse($url); @@ -15,10 +16,13 @@ public static function create($url, $autoplay = false, $width = 640, $height = 3 return null; } + $width = $width ?? ($type === self::WM_EMBED_TYPE_YOUTUBE_SHORT ? 360 : 640); + $height = $height ?? ($type === self::WM_EMBED_TYPE_YOUTUBE_SHORT ? 640 : 360); + $build = null; $lang = \Drupal::languageManager()->getCurrentLanguage()->getId(); - if ($type === self::WM_EMBED_TYPE_YOUTUBE) { + if (\in_array($type, [self::WM_EMBED_TYPE_YOUTUBE, self::WM_EMBED_TYPE_YOUTUBE_SHORT], true)) { global $base_url; $domain = $base_url; $build = [