Version 1
[yaffs-website] / web / modules / contrib / video_embed_field / src / Plugin / Field / FieldFormatter / Colorbox.php
1 <?php
2
3 namespace Drupal\video_embed_field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FormatterBase;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Field\FormatterInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Drupal\Core\Render\RendererInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Plugin implementation of the thumbnail field formatter.
16  *
17  * @FieldFormatter(
18  *   id = "video_embed_field_colorbox",
19  *   label = @Translation("Colorbox Modal"),
20  *   field_types = {
21  *     "video_embed_field"
22  *   }
23  * )
24  */
25 class Colorbox extends FormatterBase implements ContainerFactoryPluginInterface {
26
27   /**
28    * The field formatter plugin instance for thumbnails.
29    *
30    * @var \Drupal\Core\Field\FormatterInterface
31    */
32   protected $thumbnailFormatter;
33
34   /**
35    * The field formatterp plguin instance for videos.
36    *
37    * @var \Drupal\Core\Field\FormatterInterface
38    */
39   protected $videoFormatter;
40
41   /**
42    * Allow us to attach colorbox settings to our element.
43    *
44    * @var \Drupal\colorbox\ElementAttachmentInterface
45    */
46   protected $colorboxAttachment;
47
48   /**
49    * The renderer.
50    *
51    * @var \Drupal\Core\Render\RendererInterface
52    */
53   protected $renderer;
54
55   /**
56    * Constructs a new instance of the plugin.
57    *
58    * @param string $plugin_id
59    *   The plugin_id for the formatter.
60    * @param mixed $plugin_definition
61    *   The plugin implementation definition.
62    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
63    *   The definition of the field to which the formatter is associated.
64    * @param array $settings
65    *   The formatter settings.
66    * @param string $label
67    *   The formatter label display setting.
68    * @param string $view_mode
69    *   The view mode.
70    * @param array $third_party_settings
71    *   Third party settings.
72    * @param \Drupal\Core\Render\RendererInterface $renderer
73    *   The renderer.
74    * @param \Drupal\Core\Field\FormatterInterface $thumbnail_formatter
75    *   The field formatter for thumbnails.
76    * @param \Drupal\Core\Field\FormatterInterface $video_formatter
77    *   The field formatter for videos.
78    * @param \Drupal\colorbox\ElementAttachmentInterface|null $colorbox_attachment
79    *   The colorbox attachment if colorbox is enabled.
80    */
81   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, $settings, $label, $view_mode, $third_party_settings, RendererInterface $renderer, FormatterInterface $thumbnail_formatter, FormatterInterface $video_formatter, $colorbox_attachment) {
82     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
83     $this->thumbnailFormatter = $thumbnail_formatter;
84     $this->videoFormatter = $video_formatter;
85     $this->renderer = $renderer;
86     $this->colorboxAttachment = $colorbox_attachment;
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
93     $formatter_manager = $container->get('plugin.manager.field.formatter');
94     return new static(
95       $plugin_id,
96       $plugin_definition,
97       $configuration['field_definition'],
98       $configuration['settings'],
99       $configuration['label'],
100       $configuration['view_mode'],
101       $configuration['third_party_settings'],
102       $container->get('renderer'),
103       $formatter_manager->createInstance('video_embed_field_thumbnail', $configuration),
104       $formatter_manager->createInstance('video_embed_field_video', $configuration),
105       $container->get('colorbox.attachment')
106     );
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function viewElements(FieldItemListInterface $items, $langcode) {
113     $element = [];
114     $thumbnails = $this->thumbnailFormatter->viewElements($items, $langcode);
115     $videos = $this->videoFormatter->viewElements($items, $langcode);
116     foreach ($items as $delta => $item) {
117       // Support responsive videos within the colorbox modal.
118       if ($this->getSetting('responsive')) {
119         $videos[$delta]['#attributes']['class'][] = 'video-embed-field-responsive-modal';
120         $videos[$delta]['#attributes']['style'] = sprintf('width:%dpx;', $this->getSetting('modal_max_width'));
121       }
122       $element[$delta] = [
123         '#type' => 'container',
124         '#attributes' => [
125           'data-video-embed-field-modal' => (string) $this->renderer->render($videos[$delta]),
126           'class' => ['video-embed-field-launch-modal'],
127         ],
128         '#attached' => [
129           'library' => [
130             'video_embed_field/colorbox',
131             'video_embed_field/responsive-video',
132           ],
133         ],
134         // Ensure the cache context from the video formatter which was rendered
135         // early still exists in the renderable array for this formatter.
136         '#cache' => [
137           'contexts' => ['user.permissions'],
138         ],
139         'children' => $thumbnails[$delta],
140       ];
141     }
142     $this->colorboxAttachment->attach($element);
143     return $element;
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public static function defaultSettings() {
150     return Thumbnail::defaultSettings() + Video::defaultSettings() + [
151       'modal_max_width' => '854',
152     ];
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public function settingsForm(array $form, FormStateInterface $form_state) {
159     $element = parent::settingsForm($form, $form_state);
160     $element += $this->thumbnailFormatter->settingsForm([], $form_state);
161     $element += $this->videoFormatter->settingsForm([], $form_state);
162     $element['modal_max_width'] = [
163       '#title' => $this->t('Maximum Width'),
164       '#type' => 'number',
165       '#description' => $this->t('The maximum size of the video opened in the Colorbox window in pixels. For smaller screen sizes, the video will scale.'),
166       '#required' => TRUE,
167       '#field_suffix' => 'px',
168       '#size' => 20,
169       '#states' => ['visible' => [[':input[name*="responsive"]' => ['checked' => TRUE]]]],
170       '#default_value' => $this->getSetting('modal_max_width'),
171     ];
172     return $element;
173   }
174
175   /**
176    * {@inheritdoc}
177    */
178   public function settingsSummary() {
179     $summary[] = $this->t('Thumbnail that launches a modal window.');
180     $summary[] = implode(',', $this->videoFormatter->settingsSummary());
181     $summary[] = implode(',', $this->thumbnailFormatter->settingsSummary());
182     return $summary;
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   public function calculateDependencies() {
189     return parent::calculateDependencies() + $this->thumbnailFormatter->calculateDependencies() + $this->videoFormatter->calculateDependencies();
190   }
191
192   /**
193    * {@inheritdoc}
194    */
195   public function onDependencyRemoval(array $dependencies) {
196     $parent = parent::onDependencyRemoval($dependencies);
197     $thumbnail = $this->thumbnailFormatter->onDependencyRemoval($dependencies);
198     $video = $this->videoFormatter->onDependencyRemoval($dependencies);
199     $this->setSetting('image_style', $this->thumbnailFormatter->getSetting('image_style'));
200     return $parent || $thumbnail || $video;
201   }
202
203   /**
204    * {@inheritdoc}
205    */
206   public static function isApplicable(FieldDefinitionInterface $field_definition) {
207     return \Drupal::moduleHandler()->moduleExists('colorbox');
208   }
209
210 }