Skip to content

Commit

Permalink
Support YouTube Shorts
Browse files Browse the repository at this point in the history
- Remove "/shorts/" from path to get video ID
- Invert default height and width to keep vertical aspect ratio
  • Loading branch information
benjamintoussaint committed Mar 26, 2024
1 parent 3eae86d commit f13a7ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/Service/UrlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down
8 changes: 6 additions & 2 deletions src/VideoEmbedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
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);

if (!$type || !$vid) {
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 = [
Expand Down

0 comments on commit f13a7ae

Please sign in to comment.