3 namespace Drupal\video_embed_field\Plugin\Field\FieldFormatter;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FormatterBase;
8 use Drupal\Core\Field\FieldItemListInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Drupal\field\Entity\FieldConfig;
13 use Drupal\video_embed_field\ProviderManagerInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Plugin implementation of the video field formatter.
20 * id = "video_embed_field_video",
21 * label = @Translation("Video"),
27 class Video extends FormatterBase implements ContainerFactoryPluginInterface {
30 * The embed provider plugin manager.
32 * @var \Drupal\video_embed_field\ProviderManagerInterface
34 protected $providerManager;
39 * @var \Drupal\Core\Session\AccountInterface
41 protected $currentUser;
44 * Constructs a new instance of the plugin.
46 * @param string $plugin_id
47 * The plugin_id for the formatter.
48 * @param mixed $plugin_definition
49 * The plugin implementation definition.
50 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
51 * The definition of the field to which the formatter is associated.
52 * @param array $settings
53 * The formatter settings.
54 * @param string $label
55 * The formatter label display setting.
56 * @param string $view_mode
58 * @param array $third_party_settings
59 * Third party settings.
60 * @param \Drupal\video_embed_field\ProviderManagerInterface $provider_manager
61 * The video embed provider manager.
62 * @param \Drupal\Core\Session\AccountInterface $current_user
65 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, $settings, $label, $view_mode, $third_party_settings, ProviderManagerInterface $provider_manager, AccountInterface $current_user) {
66 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
67 $this->providerManager = $provider_manager;
68 $this->currentUser = $current_user;
74 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
78 $configuration['field_definition'],
79 $configuration['settings'],
80 $configuration['label'],
81 $configuration['view_mode'],
82 $configuration['third_party_settings'],
83 $container->get('video_embed_field.provider_manager'),
84 $container->get('current_user')
91 public function viewElements(FieldItemListInterface $items, $langcode) {
93 foreach ($items as $delta => $item) {
94 $provider = $this->providerManager->loadProviderFromInput($item->value);
97 $element[$delta] = ['#theme' => 'video_embed_field_missing_provider'];
100 $autoplay = $this->currentUser->hasPermission('never autoplay videos') ? FALSE : $this->getSetting('autoplay');
101 $element[$delta] = $provider->renderEmbedCode($this->getSetting('width'), $this->getSetting('height'), $autoplay);
102 $element[$delta]['#cache']['contexts'][] = 'user.permissions';
105 '#type' => 'container',
106 '#attributes' => ['class' => [Html::cleanCssIdentifier(sprintf('video-embed-field-provider-%s', $provider->getPluginId()))]],
107 'children' => $element[$delta],
110 // For responsive videos, wrap each field item in it's own container.
111 if ($this->getSetting('responsive')) {
112 $element[$delta]['#attached']['library'][] = 'video_embed_field/responsive-video';
113 $element[$delta]['#attributes']['class'][] = 'video-embed-field-responsive-video';
124 public static function defaultSettings() {
126 'responsive' => TRUE,
136 public function settingsForm(array $form, FormStateInterface $form_state) {
137 $elements = parent::settingsForm($form, $form_state);
138 $elements['autoplay'] = [
139 '#title' => $this->t('Autoplay'),
140 '#type' => 'checkbox',
141 '#description' => $this->t('Autoplay the videos for users without the "never autoplay videos" permission. Roles with this permission will bypass this setting.'),
142 '#default_value' => $this->getSetting('autoplay'),
144 $elements['responsive'] = [
145 '#title' => $this->t('Responsive Video'),
146 '#type' => 'checkbox',
147 '#description' => $this->t("Make the video fill the width of it's container, adjusting to the size of the user's screen."),
148 '#default_value' => $this->getSetting('responsive'),
150 // Loosely match the name attribute so forms which don't have a field
151 // formatter structure (such as the WYSIWYG settings form) are also matched.
152 $responsive_checked_state = [
155 ':input[name*="responsive"]' => ['checked' => FALSE],
159 $elements['width'] = [
160 '#title' => $this->t('Width'),
162 '#field_suffix' => 'px',
163 '#default_value' => $this->getSetting('width'),
166 '#states' => $responsive_checked_state,
168 $elements['height'] = [
169 '#title' => $this->t('Height'),
171 '#field_suffix' => 'px',
172 '#default_value' => $this->getSetting('height'),
175 '#states' => $responsive_checked_state,
183 public function settingsSummary() {
184 $dimensions = $this->getSetting('responsive') ? $this->t('Responsive') : $this->t('@widthx@height', ['@width' => $this->getSetting('width'), '@height' => $this->getSetting('height')]);
185 $summary[] = $this->t('Embedded Video (@dimensions@autoplay).', [
186 '@dimensions' => $dimensions,
187 '@autoplay' => $this->getSetting('autoplay') ? $this->t(', autoplaying') : '',
193 * Get an instance of the Video field formatter plugin.
195 * This is useful because there is a lot of overlap to the configuration and
196 * display of a video in a WYSIWYG and configuring a field formatter. We
197 * get an instance of the plugin with our own WYSIWYG settings shimmed in,
198 * as well as a fake field_definition because one in this context doesn't
199 * exist. This allows us to reuse aspects such as the form and settings
200 * summary for the WYSIWYG integration.
202 * @param array $settings
203 * The settings to pass to the plugin.
206 * The formatter plugin.
208 public static function mockInstance($settings) {
209 return \Drupal::service('plugin.manager.field.formatter')->createInstance('video_embed_field_video', [
210 'settings' => !empty($settings) ? $settings : [],
211 'third_party_settings' => [],
212 'field_definition' => new FieldConfig([
213 'field_name' => 'mock',
214 'entity_type' => 'mock',