3 namespace Drupal\video_embed_media\Plugin\MediaEntity\Type;
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;
17 * Provides media type plugin for video embed field.
20 * id = "video_embed_field",
21 * label = @Translation("Video embed field"),
22 * description = @Translation("Enables video_embed_field integration with media_entity.")
25 class VideoEmbedField extends MediaTypeBase {
28 * The name of the field on the media entity.
30 const VIDEO_EMBED_FIELD_DEFAULT_NAME = 'field_media_video_embed_field';
33 * The video provider manager.
35 * @var \Drupal\video_embed_field\ProviderManagerInterface
37 protected $providerManager;
42 * @var \Drupal\Core\Config\Config
44 protected $mediaSettings;
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;
58 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
74 public function thumbnail(MediaInterface $media) {
75 if ($provider = $this->loadProvider($media)) {
76 $provider->downloadThumbnail();
77 return $provider->getLocalThumbnailUri();
79 return $this->getDefaultThumbnail();
85 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
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();
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.');
96 if (!empty($options)) {
97 $form['source_field'] = [
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,
112 public function getField(MediaInterface $media, $name) {
113 if (!$url = $this->getVideoUrl($media)) {
116 $provider = $this->providerManager->loadProviderFromInput($url);
117 $definition = $this->providerManager->loadDefinitionFromInput($url);
120 return $provider->getIdFromInput($url);
123 return $definition['id'];
126 return $definition['id'];
129 case 'image_local_uri':
130 return $this->thumbnail($media);
137 public function providedFields() {
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.'),
150 public function getDefaultName(MediaInterface $media) {
151 if ($provider = $this->loadProvider($media)) {
152 return $this->loadProvider($media)->getName();
154 return parent::getDefaultThumbnail();
158 * Load a video provider given a media entity.
160 * @param \Drupal\media_entity\MediaInterface $media
163 * @return \Drupal\video_embed_field\ProviderPluginInterface
164 * The provider plugin.
166 protected function loadProvider(MediaInterface $media) {
167 $video_url = $this->getVideoUrl($media);
168 return !empty($video_url) ? $this->providerManager->loadProviderFromInput($video_url) : FALSE;
172 * Get the video URL from a media entity.
174 * @param \Drupal\media_entity\MediaInterface $media
177 * @return string|bool
178 * A video URL or FALSE on failure.
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;
187 * The function that is invoked during the insert of media bundles.
189 * @param string $media_bundle_id
190 * The ID of the media bundle.
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',
200 FieldConfig::create([
201 'entity_type' => 'media',
202 'field_name' => static::VIDEO_EMBED_FIELD_DEFAULT_NAME,
203 'label' => 'Video URL',
205 'bundle' => $media_bundle_id,
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',
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',
222 public function getDefaultThumbnail() {
223 return $this->mediaSettings->get('icon_base') . '/video.png';