deeae3ecca06f19b10f9dbe551f0b384f7ff0e28
[yaffs-website] / web / modules / contrib / video_embed_field / modules / video_embed_media / src / Plugin / MediaEntity / Type / VideoEmbedField.php
1 <?php
2
3 namespace Drupal\video_embed_media\Plugin\MediaEntity\Type;
4
5 use Drupal\Core\Entity\EntityFieldManagerInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\media_entity\MediaInterface;
11 use Drupal\media_entity\MediaTypeBase;
12 use Drupal\video_embed_field\ProviderManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Drupal\Core\Config\Config;
15
16 /**
17  * Provides media type plugin for video embed field.
18  *
19  * @MediaType(
20  *   id = "video_embed_field",
21  *   label = @Translation("Video embed field"),
22  *   description = @Translation("Enables video_embed_field integration with media_entity.")
23  * )
24  */
25 class VideoEmbedField extends MediaTypeBase {
26
27   /**
28    * The name of the field on the media entity.
29    */
30   const VIDEO_EMBED_FIELD_DEFAULT_NAME = 'field_media_video_embed_field';
31
32   /**
33    * The video provider manager.
34    *
35    * @var \Drupal\video_embed_field\ProviderManagerInterface
36    */
37   protected $providerManager;
38
39   /**
40    * The media settings.
41    *
42    * @var \Drupal\Core\Config\Config
43    */
44   protected $mediaSettings;
45
46   /**
47    * {@inheritdoc}
48    */
49   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, Config $config, ProviderManagerInterface $provider_manager, Config $media_settings) {
50     parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $config);
51     $this->providerManager = $provider_manager;
52     $this->mediaSettings = $media_settings;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
59     return new static(
60       $configuration,
61       $plugin_id,
62       $plugin_definition,
63       $container->get('entity_type.manager'),
64       $container->get('entity_field.manager'),
65       $container->get('config.factory')->get('media_entity.settings'),
66       $container->get('video_embed_field.provider_manager'),
67       $container->get('config.factory')->get('media_entity.settings')
68     );
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function thumbnail(MediaInterface $media) {
75     if ($provider = $this->loadProvider($media)) {
76       $provider->downloadThumbnail();
77       return $provider->getLocalThumbnailUri();
78     }
79     return $this->getDefaultThumbnail();
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
86
87     $options = [];
88     foreach ($this->entityFieldManager->getFieldDefinitions('media', $form_state->getFormObject()->getEntity()->id()) as $field_name => $field) {
89       if ($field->getType() == 'video_embed_field') {
90         $options[$field_name] = $field->getLabel();
91       }
92     }
93     if (empty($options)) {
94       $form['summary']['#markup'] = $this->t('A video embed field will be created on this media bundle when you save this form. You can return to this configuration screen to alter the video field used for this bundle, or you can use the one provided.');
95     }
96     if (!empty($options)) {
97       $form['source_field'] = [
98         '#type' => 'select',
99         '#required' => TRUE,
100         '#title' => $this->t('Source Video Field'),
101         '#description' => $this->t('The field on the media entity that contains the video URL.'),
102         '#default_value' => empty($this->configuration['source_field']) ? VideoEmbedField::VIDEO_EMBED_FIELD_DEFAULT_NAME : $this->configuration['source_field'],
103         '#options' => $options,
104       ];
105     }
106     return $form;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function getField(MediaInterface $media, $name) {
113     if (!$url = $this->getVideoUrl($media)) {
114       return FALSE;
115     }
116     $provider = $this->providerManager->loadProviderFromInput($url);
117     $definition = $this->providerManager->loadDefinitionFromInput($url);
118     switch ($name) {
119       case 'id':
120         return $provider->getIdFromInput($url);
121
122       case 'source':
123         return $definition['id'];
124
125       case 'source_name':
126         return $definition['id'];
127
128       case 'image_local':
129       case 'image_local_uri':
130         return $this->thumbnail($media);
131     }
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   public function providedFields() {
138     return [
139       'id' => $this->t('Video ID.'),
140       'source' => $this->t('Video source machine name.'),
141       'source_name' => $this->t('Video source human name.'),
142       'image_local' => $this->t('Copies thumbnail image to the local filesystem and returns the URI.'),
143       'image_local_uri' => $this->t('Gets URI of the locally saved image.'),
144     ];
145   }
146
147   /**
148    * {@inheritdoc}
149    */
150   public function getDefaultName(MediaInterface $media) {
151     if ($provider = $this->loadProvider($media)) {
152       return $this->loadProvider($media)->getName();
153     }
154     return parent::getDefaultThumbnail();
155   }
156
157   /**
158    * Load a video provider given a media entity.
159    *
160    * @param \Drupal\media_entity\MediaInterface $media
161    *   The media entity.
162    *
163    * @return \Drupal\video_embed_field\ProviderPluginInterface
164    *   The provider plugin.
165    */
166   protected function loadProvider(MediaInterface $media) {
167     $video_url = $this->getVideoUrl($media);
168     return !empty($video_url) ? $this->providerManager->loadProviderFromInput($video_url) : FALSE;
169   }
170
171   /**
172    * Get the video URL from a media entity.
173    *
174    * @param \Drupal\media_entity\MediaInterface $media
175    *   The media entity.
176    *
177    * @return string|bool
178    *   A video URL or FALSE on failure.
179    */
180   protected function getVideoUrl(MediaInterface $media) {
181     $field_name = empty($this->configuration['source_field']) ? VideoEmbedField::VIDEO_EMBED_FIELD_DEFAULT_NAME : $this->configuration['source_field'];
182     $video_url = $media->{$field_name}->value;
183     return !empty($video_url) ? $video_url : FALSE;
184   }
185
186   /**
187    * The function that is invoked during the insert of media bundles.
188    *
189    * @param string $media_bundle_id
190    *   The ID of the media bundle.
191    */
192   public static function createVideoEmbedField($media_bundle_id) {
193     if (!$storage = FieldStorageConfig::loadByName('media', static::VIDEO_EMBED_FIELD_DEFAULT_NAME)) {
194       FieldStorageConfig::create([
195         'field_name' => static::VIDEO_EMBED_FIELD_DEFAULT_NAME,
196         'entity_type' => 'media',
197         'type' => 'video_embed_field',
198       ])->save();
199     }
200     FieldConfig::create([
201       'entity_type' => 'media',
202       'field_name' => static::VIDEO_EMBED_FIELD_DEFAULT_NAME,
203       'label' => 'Video URL',
204       'required' => TRUE,
205       'bundle' => $media_bundle_id,
206     ])->save();
207     // Make the field visible on the form display.
208     $form_display = entity_get_form_display('media', $media_bundle_id, 'default');
209     $form_display->setComponent(static::VIDEO_EMBED_FIELD_DEFAULT_NAME, [
210       'type' => 'video_embed_field_textfield',
211     ])->save();
212     // Make the field visible on the media entity itself.
213     $dispaly = entity_get_display('media', $media_bundle_id, 'default');
214     $dispaly->setComponent(static::VIDEO_EMBED_FIELD_DEFAULT_NAME, [
215       'type' => 'video_embed_field_video',
216     ])->save();
217   }
218
219   /**
220    * {@inheritdoc}
221    */
222   public function getDefaultThumbnail() {
223     return $this->mediaSettings->get('icon_base') . '/video.png';
224   }
225
226 }