Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / video / src / ProviderPluginBase.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\video\ProviderPluginBase
6  */
7
8 namespace Drupal\video;
9
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;
15
16 /**
17  * A base for the provider plugins.
18  */
19 abstract class ProviderPluginBase implements ProviderPluginInterface, ContainerFactoryPluginInterface {
20
21   /**
22    * File object to handle
23    *
24    * @var Drupal\file\Entity\File $file
25    */
26   protected $file;
27
28   /**
29    * Additional metadata for the embedded video object
30    *
31    * @var array
32    */
33   protected $metadata = array();
34
35   /**
36    * Additional settings for the video widget
37    *
38    * @var array
39    */
40   protected $settings = array();
41   
42   /**
43    * An http client.
44    *
45    * @var \GuzzleHttp\ClientInterface
46    */
47   protected $httpClient;
48
49   /**
50    * Create a plugin with the given input.
51    *
52    * @param string $configuration
53    *   The configuration of the plugin.
54    * @param \GuzzleHttp\ClientInterface $http_client
55    *    An HTTP client.
56    *
57    * @throws \Exception
58    */
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;
64   }
65
66   /**
67    * Get the ID of the video.
68    *
69    * @return string
70    *   The video ID.
71    */
72   protected function getVideoFile() {
73     return $this->file;
74   }
75
76   /**
77    * Get the input which caused this plugin to be selected.
78    *
79    * @return string
80    *   The raw input from the user.
81    */
82   protected function getVideoMetadata() {
83     return $this->metadata;
84   }
85
86   /**
87    * Get the input which caused this plugin to be selected.
88    *
89    * @return string
90    *   The raw input from the user.
91    */
92   protected function getVideoSettings() {
93     return $this->settings;
94   }
95   
96   /**
97    * {@inheritdoc}
98    */
99   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
100     return new static($configuration, $container->get('http_client'));
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function renderThumbnail($image_style, $link_url) {
107     $this->downloadThumbnail();
108     $output = [
109       '#theme' => 'image',
110       '#uri' => !empty($image_style) ? ImageStyle::load($image_style)->buildUrl($this->getLocalThumbnailUri()) : $this->getLocalThumbnailUri(),
111     ];
112     if ($link_url) {
113       $output = [
114         '#type' => 'link',
115         '#title' => $output,
116         '#url' => $link_url,
117       ];
118     }
119     return $output;
120   }
121
122   /**
123    * Download the remote thumbnail to the local file system.
124    */
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);
132     }
133   }
134   
135   /**
136    * Get the URL to the local thumbnail.
137    *
138    * @return string
139    *   The URI for the local thumbnail.
140    */
141   public function getLocalThumbnailUri() {
142     $data = $this->getVideoMetadata();
143     return $this->getUploadLocation() . '/' . $data['id'] . '.png';
144   }
145   
146   /**
147    * Determines the URI for a video field.
148    *
149    * @param array $settings
150    *   The array of field settings.
151    * @param array $data
152    *   An array of token objects to pass to token_replace().
153    *
154    * @return string
155    *   An unsanitized file directory URI with tokens replaced. The result of
156    *   the token replacement is then converted to plain text and returned.
157    */
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;
163   }
164 }