abstract class ProviderPluginBase in Video Embed Field 8
Same name and namespace in other branches
- 8.2 src/ProviderPluginBase.php \Drupal\video_embed_field\ProviderPluginBase
A base for the provider plugins.
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\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ProviderPluginBase
3 files declare their use of ProviderPluginBase
- Vimeo.php in src/
Plugin/ video_embed_field/ Provider/ Vimeo.php - YouTube.php in src/
Plugin/ video_embed_field/ Provider/ YouTube.php - YouTubePlaylist.php in src/
Plugin/ video_embed_field/ Provider/ YouTubePlaylist.php
File
- src/
ProviderPluginBase.php, line 14
Namespace
Drupal\video_embed_fieldView source
abstract class ProviderPluginBase extends PluginBase implements ProviderPluginInterface, ContainerFactoryPluginInterface {
/**
* The directory where thumbnails are stored.
*
* @var string
*/
protected $thumbsDirectory = 'public://video_thumbnails';
/**
* The ID of the video.
*
* @var string
*/
protected $videoId;
/**
* The input that caused the embed provider to be selected.
*
* @var string
*/
protected $input;
/**
* An http client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Create a plugin with the given input.
*
* @param array $configuration
* The configuration of the plugin.
* @param string $plugin_id
* The plugin id.
* @param array $plugin_definition
* The plugin definition.
* @param \GuzzleHttp\ClientInterface $http_client
* An HTTP client.
*
* @throws \Exception
*/
public function __construct($configuration, $plugin_id, $plugin_definition, ClientInterface $http_client) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
if (!static::isApplicable($configuration['input'])) {
throw new \Exception('Tried to create a video provider plugin with invalid input.');
}
$this->input = $configuration['input'];
$this->videoId = $this
->getIdFromInput($configuration['input']);
$this->httpClient = $http_client;
}
/**
* Get the ID of the video.
*
* @return string
* The video ID.
*/
protected function getVideoId() {
return $this->videoId;
}
/**
* Get the file system service.
*
* @return \Drupal\Core\File\FileSystemInterface
* The file system service.
*/
protected function getFileSystem() {
if (!isset($this->fileSystem)) {
$this->fileSystem = \Drupal::service('file_system');
}
return $this->fileSystem;
}
/**
* Get the input which caused this plugin to be selected.
*
* @return string
* The raw input from the user.
*/
protected function getInput() {
return $this->input;
}
/**
* {@inheritdoc}
*/
public static function isApplicable($input) {
$id = static::getIdFromInput($input);
return !empty($id);
}
/**
* {@inheritdoc}
*/
public function renderThumbnail($image_style, $link_url) {
$output = [
'#theme' => 'image',
'#uri' => $this
->getLocalThumbnailUri(),
];
if (!empty($image_style)) {
$output['#theme'] = 'image_style';
$output['#style_name'] = $image_style;
}
if ($link_url) {
$output = [
'#type' => 'link',
'#title' => $output,
'#url' => $link_url,
];
}
return $output;
}
/**
* {@inheritdoc}
*/
public function downloadThumbnail() {
$local_uri = $this
->getLocalThumbnailUri();
if (!file_exists($local_uri)) {
$this
->getFileSystem()
->prepareDirectory($this->thumbsDirectory, FileSystemInterface::CREATE_DIRECTORY);
try {
$thumbnail = $this->httpClient
->request('GET', $this
->getRemoteThumbnailUrl());
$this
->getFileSystem()
->saveData((string) $thumbnail
->getBody(), $local_uri);
} catch (\Exception $e) {
}
}
}
/**
* {@inheritdoc}
*/
public function getLocalThumbnailUri() {
return $this->thumbsDirectory . '/' . $this
->getVideoId() . '.jpg';
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('http_client'));
}
/**
* {@inheritdoc}
*/
public function getName() {
return $this
->t('@provider Video (@id)', [
'@provider' => $this
->getPluginDefinition()['title'],
'@id' => $this
->getVideoId(),
]);
}
}
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:: |
|
ProviderPluginInterface:: |
public static | function | Get the ID of the video from user input. | 4 |
ProviderPluginInterface:: |
public | function | Get the URL of the remote thumbnail. | 4 |
ProviderPluginInterface:: |
public | function | Render embed code. | 4 |
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. |