Version 1
[yaffs-website] / web / modules / contrib / video / src / Plugin / Field / FieldFormatter / VideoEmbedPlayerFormatter.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\video\Plugin\Field\FieldFormatter\VideoEmbedPlayerFormatter.
6  */
7
8 namespace Drupal\video\Plugin\Field\FieldFormatter;
9
10 use Drupal\Core\Field\FormatterBase;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14 use Drupal\video\ProviderManagerInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Drupal\file\Entity\File;
17 use Drupal\Core\File\FileSystem;
18 use Drupal\Core\Field\FieldDefinitionInterface;
19
20 /**
21  * Plugin implementation of the video field formatter.
22  *
23  * @FieldFormatter(
24  *   id = "video_embed_player",
25  *   label = @Translation("Embedded Video Player"),
26  *   field_types = {
27  *     "video"
28  *   }
29  * )
30  */
31 class VideoEmbedPlayerFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
32
33   /**
34    * The embed provider plugin manager.
35    *
36    * @var \Drupal\video\ProviderManagerInterface
37    */
38   protected $providerManager;
39
40   /**
41    * {@inheritdoc}
42    */
43   public function viewElements(FieldItemListInterface $items, $langcode) {
44     $element = [];
45     $settings = $this->getSettings();
46     foreach ($items as $delta => $item) {
47       $file = File::load($item->target_id);
48       if(!$file) continue;
49       $metadata = isset($item->data) ? unserialize($item->data) : array();
50       $scheme = file_uri_scheme($file->getFileUri());
51       $provider = $this->providerManager->loadProviderFromStream($scheme, $file, $metadata);
52       if($provider){
53         $element[$delta] = $provider->renderEmbedCode($settings);
54       }
55     }
56     return $element;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function defaultSettings() {
63     return [
64       'width' => '854',
65       'height' => '480',
66       'autoplay' => TRUE,
67     ];
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function settingsForm(array $form, FormStateInterface $form_state) {
74     $form['autoplay'] = [
75       '#title' => t('Autoplay'),
76       '#type' => 'checkbox',
77       '#default_value' => $this->getSetting('autoplay'),
78     ];
79     $form['width'] = [
80       '#title' => t('Width'),
81       '#type' => 'textfield',
82       '#default_value' => $this->getSetting('width'),
83       '#required' => TRUE,
84     ];
85     $form['height'] = [
86       '#title' => t('Height'),
87       '#type' => 'textfield',
88       '#default_value' => $this->getSetting('height'),
89       '#required' => TRUE,
90     ];
91     return $form;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function settingsSummary() {
98     $summary[] = t('Embedded Video (@widthx@height@autoplay).', [
99       '@width' => $this->getSetting('width'),
100       '@height' => $this->getSetting('height'),
101       '@autoplay' => $this->getSetting('autoplay') ? t(', autoplaying') : '' ,
102     ]);
103     return $summary;
104   }
105
106   /**
107    * Constructs a new instance of the plugin.
108    *
109    * @param string $plugin_id
110    *   The plugin_id for the formatter.
111    * @param mixed $plugin_definition
112    *   The plugin implementation definition.
113    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
114    *   The definition of the field to which the formatter is associated.
115    * @param array $settings
116    *   The formatter settings.
117    * @param string $label
118    *   The formatter label display setting.
119    * @param string $view_mode
120    *   The view mode.
121    * @param array $third_party_settings
122    *   Third party settings.
123    * @param \Drupal\video\ProviderManagerInterface $provider_manager
124    *   The video embed provider manager.
125    */
126   public function __construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, ProviderManagerInterface $provider_manager) {
127     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
128     $this->providerManager = $provider_manager;
129   }
130
131   /**
132    * {@inheritdoc}
133    */
134   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
135     return new static(
136       $plugin_id,
137       $plugin_definition,
138       $configuration['field_definition'],
139       $configuration['settings'],
140       $configuration['label'],
141       $configuration['view_mode'],
142       $configuration['third_party_settings'],
143       $container->get('video.provider_manager')
144     );
145   }
146   
147   /**
148    * {@inheritdoc}
149    */
150   public static function isApplicable(FieldDefinitionInterface $field_definition) {
151     if(empty($field_definition->getTargetBundle())){
152       return TRUE;
153     }
154     else{
155       $entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default');
156       $widget = $entity_form_display->getRenderer($field_definition->getName());
157       $widget_id = $widget->getBaseId();
158       if($widget_id == 'video_embed'){
159         return TRUE;
160       }
161     }
162     return FALSE;
163   }
164 }