5 * Contains Drupal\video\ProviderPluginBase
8 namespace Drupal\video;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Drupal\Component\Render\PlainTextOutput;
13 use GuzzleHttp\ClientInterface;
14 use Drupal\image\Entity\ImageStyle;
17 * A base for the provider plugins.
19 abstract class ProviderPluginBase implements ProviderPluginInterface, ContainerFactoryPluginInterface {
22 * File object to handle
24 * @var Drupal\file\Entity\File $file
29 * Additional metadata for the embedded video object
33 protected $metadata = array();
36 * Additional settings for the video widget
40 protected $settings = array();
45 * @var \GuzzleHttp\ClientInterface
47 protected $httpClient;
50 * Create a plugin with the given input.
52 * @param string $configuration
53 * The configuration of the plugin.
54 * @param \GuzzleHttp\ClientInterface $http_client
59 public function __construct($configuration, ClientInterface $http_client) {
60 $this->file = $configuration['file'];
61 $this->metadata = $configuration['metadata'];
62 $this->settings = $configuration['settings'];
63 $this->httpClient = $http_client;
67 * Get the ID of the video.
72 protected function getVideoFile() {
77 * Get the input which caused this plugin to be selected.
80 * The raw input from the user.
82 protected function getVideoMetadata() {
83 return $this->metadata;
87 * Get the input which caused this plugin to be selected.
90 * The raw input from the user.
92 protected function getVideoSettings() {
93 return $this->settings;
99 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
100 return new static($configuration, $container->get('http_client'));
106 public function renderThumbnail($image_style, $link_url) {
107 $this->downloadThumbnail();
110 '#uri' => !empty($image_style) ? ImageStyle::load($image_style)->buildUrl($this->getLocalThumbnailUri()) : $this->getLocalThumbnailUri(),
123 * Download the remote thumbnail to the local file system.
125 protected function downloadThumbnail() {
126 $local_uri = $this->getLocalThumbnailUri();
127 if (!file_exists($local_uri)) {
128 $thumb_dir = $this->getUploadLocation();
129 file_prepare_directory($thumb_dir, FILE_CREATE_DIRECTORY);
130 $thumbnail = $this->httpClient->request('GET', $this->getRemoteThumbnailUrl());
131 file_unmanaged_save_data((string) $thumbnail->getBody(), $local_uri);
136 * Get the URL to the local thumbnail.
139 * The URI for the local thumbnail.
141 public function getLocalThumbnailUri() {
142 $data = $this->getVideoMetadata();
143 return $this->getUploadLocation() . '/' . $data['id'] . '.png';
147 * Determines the URI for a video field.
149 * @param array $settings
150 * The array of field settings.
152 * An array of token objects to pass to token_replace().
155 * An unsanitized file directory URI with tokens replaced. The result of
156 * the token replacement is then converted to plain text and returned.
158 protected function getUploadLocation($data = []) {
159 $settings = $this->getVideoSettings();
160 $destination = trim($settings['file_directory'], '/');
161 $destination = PlainTextOutput::renderFromHtml(\Drupal::token()->replace($destination, $data));
162 return $settings['uri_scheme'] . '://' . $destination;