class YouTube in Video Embed Field 8
Same name and namespace in other branches
- 8.2 src/Plugin/video_embed_field/Provider/YouTube.php \Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube
A YouTube provider plugin.
Plugin annotation
@VideoEmbedProvider(
id = "youtube",
title = @Translation("YouTube")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\video_embed_field\ProviderPluginBase implements ContainerFactoryPluginInterface, ProviderPluginInterface
- class \Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube
- class \Drupal\video_embed_field\ProviderPluginBase implements ContainerFactoryPluginInterface, ProviderPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of YouTube
1 file declares its use of YouTube
- ProviderUrlParseTest.php in tests/
src/ Unit/ ProviderUrlParseTest.php
File
- src/
Plugin/ video_embed_field/ Provider/ YouTube.php, line 15
Namespace
Drupal\video_embed_field\Plugin\video_embed_field\ProviderView source
class YouTube extends ProviderPluginBase {
/**
* {@inheritdoc}
*/
public function renderEmbedCode($width, $height, $autoplay) {
$embed_code = [
'#type' => 'video_embed_iframe',
'#provider' => 'youtube',
'#url' => sprintf('https://www.youtube.com/embed/%s', $this
->getVideoId()),
'#query' => [
'autoplay' => $autoplay,
'start' => $this
->getTimeIndex(),
'rel' => '0',
],
'#attributes' => [
'width' => $width,
'height' => $height,
'frameborder' => '0',
'allowfullscreen' => 'allowfullscreen',
],
];
if ($language = $this
->getLanguagePreference()) {
$embed_code['#query']['cc_lang_pref'] = $language;
}
return $embed_code;
}
/**
* Get the time index for when the given video starts.
*
* @return int
* The time index where the video should start based on the URL.
*/
protected function getTimeIndex() {
preg_match('/[&\\?]t=((?<hours>\\d+)h)?((?<minutes>\\d+)m)?(?<seconds>\\d+)s?/', $this
->getInput(), $matches);
$hours = !empty($matches['hours']) ? $matches['hours'] : 0;
$minutes = !empty($matches['minutes']) ? $matches['minutes'] : 0;
$seconds = !empty($matches['seconds']) ? $matches['seconds'] : 0;
return $hours * 3600 + $minutes * 60 + $seconds;
}
/**
* Extract the language preference from the URL for use in closed captioning.
*
* @return string|FALSE
* The language preference if one exists or FALSE if one could not be found.
*/
protected function getLanguagePreference() {
preg_match('/[&\\?]hl=(?<language>[a-z\\-]*)/', $this
->getInput(), $matches);
return isset($matches['language']) ? $matches['language'] : FALSE;
}
/**
* {@inheritdoc}
*/
public function getRemoteThumbnailUrl() {
$url = 'http://img.youtube.com/vi/%s/%s.jpg';
$high_resolution = sprintf($url, $this
->getVideoId(), 'maxresdefault');
$backup = sprintf($url, $this
->getVideoId(), 'mqdefault');
try {
$this->httpClient
->head($high_resolution);
return $high_resolution;
} catch (\Exception $e) {
return $backup;
}
}
/**
* {@inheritdoc}
*/
public static function getIdFromInput($input) {
preg_match('/^https?:\\/\\/(www\\.)?((?!.*list=)youtube\\.com\\/watch\\?.*v=|youtu\\.be\\/)(?<id>[0-9A-Za-z_-]*)/', $input, $matches);
return isset($matches['id']) ? $matches['id'] : FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ProviderPluginBase:: |
protected | property | ||
ProviderPluginBase:: |
protected | property | An http client. | |
ProviderPluginBase:: |
protected | property | The input that caused the embed provider to be selected. | |
ProviderPluginBase:: |
protected | property | The directory where thumbnails are stored. | |
ProviderPluginBase:: |
protected | property | The ID of the video. | |
ProviderPluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
ProviderPluginBase:: |
public | function |
Download the remote thumbnail URL to the local thumbnail URI. Overrides ProviderPluginInterface:: |
|
ProviderPluginBase:: |
protected | function | Get the file system service. | |
ProviderPluginBase:: |
protected | function | Get the input which caused this plugin to be selected. | |
ProviderPluginBase:: |
public | function |
Get the URL to the local thumbnail. Overrides ProviderPluginInterface:: |
|
ProviderPluginBase:: |
public | function |
Get the name of the video. Overrides ProviderPluginInterface:: |
1 |
ProviderPluginBase:: |
protected | function | Get the ID of the video. | |
ProviderPluginBase:: |
public static | function |
Check if the plugin is applicable to the user input. Overrides ProviderPluginInterface:: |
|
ProviderPluginBase:: |
public | function |
Render a thumbnail. Overrides ProviderPluginInterface:: |
|
ProviderPluginBase:: |
public | function |
Create a plugin with the given input. Overrides PluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
YouTube:: |
public static | function |
Get the ID of the video from user input. Overrides ProviderPluginInterface:: |
|
YouTube:: |
protected | function | Extract the language preference from the URL for use in closed captioning. | |
YouTube:: |
public | function |
Get the URL of the remote thumbnail. Overrides ProviderPluginInterface:: |
|
YouTube:: |
protected | function | Get the time index for when the given video starts. | |
YouTube:: |
public | function |
Render embed code. Overrides ProviderPluginInterface:: |