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\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\field\Entity\FieldConfig;
12 use Drupal\video_embed_field\ProviderManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Plugin implementation of the video field formatter.
19 * id = "video_embed_field_video",
20 * label = @Translation("Video"),
26 class Video extends FormatterBase implements ContainerFactoryPluginInterface {
29 * The embed provider plugin manager.
31 * @var \Drupal\video_embed_field\ProviderManagerInterface
33 protected $providerManager;
38 * @var \Drupal\Core\Session\AccountInterface
40 protected $currentUser;
43 * Constructs a new instance of the plugin.
45 * @param string $plugin_id
46 * The plugin_id for the formatter.
47 * @param mixed $plugin_definition
48 * The plugin implementation definition.
49 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
50 * The definition of the field to which the formatter is associated.
51 * @param array $settings
52 * The formatter settings.
53 * @param string $label
54 * The formatter label display setting.
55 * @param string $view_mode
57 * @param array $third_party_settings
58 * Third party settings.
59 * @param \Drupal\video_embed_field\ProviderManagerInterface $provider_manager
60 * The video embed provider manager.
61 * @param \Drupal\Core\Session\AccountInterface $current_user
64 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, $settings, $label, $view_mode, $third_party_settings, ProviderManagerInterface $provider_manager, AccountInterface $current_user) {
65 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
66 $this->providerManager = $provider_manager;
67 $this->currentUser = $current_user;
73 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
77 $configuration['field_definition'],
78 $configuration['settings'],
79 $configuration['label'],
80 $configuration['view_mode'],
81 $configuration['third_party_settings'],
82 $container->get('video_embed_field.provider_manager'),
83 $container->get('current_user')
90 public function viewElements(FieldItemListInterface $items, $langcode) {
92 foreach ($items as $delta => $item) {
93 $provider = $this->providerManager->loadProviderFromInput($item->value);
96 $element[$delta] = ['#theme' => 'video_embed_field_missing_provider'];
99 $autoplay = $this->currentUser->hasPermission('never autoplay videos') ? FALSE : $this->getSetting('autoplay');
100 $element[$delta] = $provider->renderEmbedCode($this->getSetting('width'), $this->getSetting('height'), $autoplay);
101 $element[$delta]['#cache']['contexts'][] = 'user.permissions';
103 // For responsive videos, wrap each field item in it's own container.
104 if ($this->getSetting('responsive')) {
106 '#type' => 'container',
107 '#attached' => ['library' => ['video_embed_field/responsive-video']],
108 '#attributes' => ['class' => ['video-embed-field-responsive-video']],
109 'children' => $element[$delta],
121 public static function defaultSettings() {
123 'responsive' => TRUE,
133 public function settingsForm(array $form, FormStateInterface $form_state) {
134 $elements = parent::settingsForm($form, $form_state);
135 $elements['autoplay'] = [
136 '#title' => $this->t('Autoplay'),
137 '#type' => 'checkbox',
138 '#description' => $this->t('Autoplay the videos for users without the "never autoplay videos" permission. Roles with this permission will bypass this setting.'),
139 '#default_value' => $this->getSetting('autoplay'),
141 $elements['responsive'] = [
142 '#title' => $this->t('Responsive Video'),
143 '#type' => 'checkbox',
144 '#description' => $this->t("Make the video fill the width of it's container, adjusting to the size of the user's screen."),
145 '#default_value' => $this->getSetting('responsive'),
147 // Loosely match the name attribute so forms which don't have a field
148 // formatter structure (such as the WYSIWYG settings form) are also matched.
149 $responsive_checked_state = [
152 ':input[name*="responsive"]' => ['checked' => FALSE],
156 $elements['width'] = [
157 '#title' => $this->t('Width'),
159 '#field_suffix' => 'px',
160 '#default_value' => $this->getSetting('width'),
163 '#states' => $responsive_checked_state,
165 $elements['height'] = [
166 '#title' => $this->t('Height'),
168 '#field_suffix' => 'px',
169 '#default_value' => $this->getSetting('height'),
172 '#states' => $responsive_checked_state,
180 public function settingsSummary() {
181 $dimensions = $this->getSetting('responsive') ? $this->t('Responsive') : $this->t('@widthx@height', ['@width' => $this->getSetting('width'), '@height' => $this->getSetting('height')]);
182 $summary[] = $this->t('Embedded Video (@dimensions@autoplay).', [
183 '@dimensions' => $dimensions,
184 '@autoplay' => $this->getSetting('autoplay') ? $this->t(', autoplaying') : '',
190 * Get an instance of the Video field formatter plugin.
192 * This is useful because there is a lot of overlap to the configuration and
193 * display of a video in a WYSIWYG and configuring a field formatter. We
194 * get an instance of the plugin with our own WYSIWYG settings shimmed in,
195 * as well as a fake field_definition because one in this context doesn't
196 * exist. This allows us to reuse aspects such as the form and settings
197 * summary for the WYSIWYG integration.
199 * @param array $settings
200 * The settings to pass to the plugin.
203 * The formatter plugin.
205 public static function mockInstance($settings) {
206 return \Drupal::service('plugin.manager.field.formatter')->createInstance('video_embed_field_video', [
207 'settings' => !empty($settings) ? $settings : [],
208 'third_party_settings' => [],
209 'field_definition' => new FieldConfig([
210 'field_name' => 'mock',
211 'entity_type' => 'mock',