3 namespace Drupal\video_embed_field\Plugin\Field\FieldFormatter;
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;
15 * Plugin implementation of the thumbnail field formatter.
18 * id = "video_embed_field_colorbox",
19 * label = @Translation("Colorbox Modal"),
25 class Colorbox extends FormatterBase implements ContainerFactoryPluginInterface {
28 * The field formatter plugin instance for thumbnails.
30 * @var \Drupal\Core\Field\FormatterInterface
32 protected $thumbnailFormatter;
35 * The field formatterp plguin instance for videos.
37 * @var \Drupal\Core\Field\FormatterInterface
39 protected $videoFormatter;
42 * Allow us to attach colorbox settings to our element.
44 * @var \Drupal\colorbox\ElementAttachmentInterface
46 protected $colorboxAttachment;
51 * @var \Drupal\Core\Render\RendererInterface
56 * Constructs a new instance of the plugin.
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
70 * @param array $third_party_settings
71 * Third party settings.
72 * @param \Drupal\Core\Render\RendererInterface $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.
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;
92 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
93 $formatter_manager = $container->get('plugin.manager.field.formatter');
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')
112 public function viewElements(FieldItemListInterface $items, $langcode) {
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'));
123 '#type' => 'container',
125 'data-video-embed-field-modal' => (string) $this->renderer->render($videos[$delta]),
126 'class' => ['video-embed-field-launch-modal'],
130 'video_embed_field/colorbox',
131 'video_embed_field/responsive-video',
134 // Ensure the cache context from the video formatter which was rendered
135 // early still exists in the renderable array for this formatter.
137 'contexts' => ['user.permissions'],
139 'children' => $thumbnails[$delta],
142 $this->colorboxAttachment->attach($element);
149 public static function defaultSettings() {
150 return Thumbnail::defaultSettings() + Video::defaultSettings() + [
151 'modal_max_width' => '854',
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'),
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.'),
167 '#field_suffix' => 'px',
169 '#states' => ['visible' => [[':input[name*="responsive"]' => ['checked' => TRUE]]]],
170 '#default_value' => $this->getSetting('modal_max_width'),
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());
188 public function calculateDependencies() {
189 return parent::calculateDependencies() + $this->thumbnailFormatter->calculateDependencies() + $this->videoFormatter->calculateDependencies();
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;
206 public static function isApplicable(FieldDefinitionInterface $field_definition) {
207 return \Drupal::moduleHandler()->moduleExists('colorbox');