Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / video_embed_field / src / ProviderPluginBase.php
1 <?php
2
3 namespace Drupal\video_embed_field;
4
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\Core\Plugin\PluginBase;
7 use GuzzleHttp\ClientInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * A base for the provider plugins.
12  */
13 abstract class ProviderPluginBase extends PluginBase implements ProviderPluginInterface, ContainerFactoryPluginInterface {
14
15   /**
16    * The directory where thumbnails are stored.
17    *
18    * @var string
19    */
20   protected $thumbsDirectory = 'public://video_thumbnails';
21
22   /**
23    * The ID of the video.
24    *
25    * @var string
26    */
27   protected $videoId;
28
29   /**
30    * The input that caused the embed provider to be selected.
31    *
32    * @var string
33    */
34   protected $input;
35
36   /**
37    * An http client.
38    *
39    * @var \GuzzleHttp\ClientInterface
40    */
41   protected $httpClient;
42
43   /**
44    * Create a plugin with the given input.
45    *
46    * @param string $configuration
47    *   The configuration of the plugin.
48    * @param string $plugin_id
49    *   The plugin id.
50    * @param array $plugin_definition
51    *   The plugin definition.
52    * @param \GuzzleHttp\ClientInterface $http_client
53    *    An HTTP client.
54    *
55    * @throws \Exception
56    */
57   public function __construct($configuration, $plugin_id, $plugin_definition, ClientInterface $http_client) {
58     parent::__construct($configuration, $plugin_id, $plugin_definition);
59     if (!static::isApplicable($configuration['input'])) {
60       throw new \Exception('Tried to create a video provider plugin with invalid input.');
61     }
62     $this->input = $configuration['input'];
63     $this->videoId = $this->getIdFromInput($configuration['input']);
64     $this->httpClient = $http_client;
65   }
66
67   /**
68    * Get the ID of the video.
69    *
70    * @return string
71    *   The video ID.
72    */
73   protected function getVideoId() {
74     return $this->videoId;
75   }
76
77   /**
78    * Get the input which caused this plugin to be selected.
79    *
80    * @return string
81    *   The raw input from the user.
82    */
83   protected function getInput() {
84     return $this->input;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public static function isApplicable($input) {
91     $id = static::getIdFromInput($input);
92     return !empty($id);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function renderThumbnail($image_style, $link_url) {
99     $output = [
100       '#theme' => 'image',
101       '#uri' => $this->getLocalThumbnailUri(),
102     ];
103
104     if (!empty($image_style)) {
105       $output['#theme'] = 'image_style';
106       $output['#style_name'] = $image_style;
107     }
108
109     if ($link_url) {
110       $output = [
111         '#type' => 'link',
112         '#title' => $output,
113         '#url' => $link_url,
114       ];
115     }
116     return $output;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function downloadThumbnail() {
123     $local_uri = $this->getLocalThumbnailUri();
124     if (!file_exists($local_uri)) {
125       file_prepare_directory($this->thumbsDirectory, FILE_CREATE_DIRECTORY);
126       try {
127         $thumbnail = $this->httpClient->request('GET', $this->getRemoteThumbnailUrl());
128         file_unmanaged_save_data((string) $thumbnail->getBody(), $local_uri);
129       }
130       catch (\Exception $e) {
131       }
132     }
133   }
134
135   /**
136    * {@inheritdoc}
137    */
138   public function getLocalThumbnailUri() {
139     return $this->thumbsDirectory . '/' . $this->getVideoId() . '.jpg';
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
146     return new static($configuration, $plugin_id, $plugin_definition, $container->get('http_client'));
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function getName() {
153     return $this->t('@provider Video (@id)', ['@provider' => $this->getPluginDefinition()['title'], '@id' => $this->getVideoId()]);
154   }
155
156 }