5 * Contains \Drupal\video\Plugin\Field\FieldFormatter\VidePlayerFormatter.
8 namespace Drupal\video\Plugin\Field\FieldFormatter;
10 use Drupal\Core\Field\FieldItemListInterface;
11 use Drupal\Core\Field\FieldDefinitionInterface;
13 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14 use Drupal\Core\Session\AccountInterface;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Drupal\Core\Form\FormStateInterface;
18 use Drupal\Core\Cache\Cache;
21 * Plugin implementation of the 'video_player' formatter.
24 * id = "video_player",
25 * label = @Translation("HTML5 Video Player"),
32 class VideoPlayerFormatter extends VideoPlayerFormatterBase implements ContainerFactoryPluginInterface {
37 * @var \Drupal\Core\Session\AccountInterface
39 protected $currentUser;
42 * Constructs an VideoPlayerFormatter object.
44 * @param string $plugin_id
45 * The plugin_id for the formatter.
46 * @param mixed $plugin_definition
47 * The plugin implementation definition.
48 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
49 * The definition of the field to which the formatter is associated.
50 * @param array $settings
51 * The formatter settings.
52 * @param string $label
53 * The formatter label display setting.
54 * @param string $view_mode
56 * @param array $third_party_settings
57 * Any third party settings settings.
58 * @param \Drupal\Core\Session\AccountInterface $current_user
61 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user) {
62 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
63 $this->currentUser = $current_user;
69 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
73 $configuration['field_definition'],
74 $configuration['settings'],
75 $configuration['label'],
76 $configuration['view_mode'],
77 $configuration['third_party_settings'],
78 $container->get('current_user')
85 public static function defaultSettings() {
94 ) + parent::defaultSettings();
100 public function settingsForm(array $form, FormStateInterface $form_state) {
101 $element['width'] = [
102 '#title' => t('Width'),
103 '#type' => 'textfield',
104 '#default_value' => $this->getSetting('width'),
107 $element['height'] = [
108 '#title' => t('Height'),
109 '#type' => 'textfield',
110 '#default_value' => $this->getSetting('height'),
113 $element['controls'] = [
114 '#title' => t('Show controls'),
115 '#type' => 'checkbox',
116 '#default_value' => $this->getSetting('controls'),
118 $element['autoplay'] = [
119 '#title' => t('Autoplay'),
120 '#type' => 'checkbox',
121 '#default_value' => $this->getSetting('autoplay'),
124 '#title' => t('Loop'),
125 '#type' => 'checkbox',
126 '#default_value' => $this->getSetting('loop'),
128 $element['muted'] = [
129 '#title' => t('Muted'),
130 '#type' => 'checkbox',
131 '#default_value' => $this->getSetting('muted'),
133 $element['preload'] = [
134 '#title' => t('Preload'),
136 '#default_value' => $this->getSetting('preload'),
139 'metadata' => 'metadata',
142 '#description' => t('Hint to the browser about whether optimistic downloading of the video itself or its metadata is considered worthwhile.')
150 public function settingsSummary() {
152 $summary[] = t('HTML5 Video (@widthx@height@controls@autoplay@loop@muted).', [
153 '@width' => $this->getSetting('width'),
154 '@height' => $this->getSetting('height'),
155 '@controls' => $this->getSetting('controls') ? t(', controls') : '' ,
156 '@autoplay' => $this->getSetting('autoplay') ? t(', autoplaying') : '' ,
157 '@loop' => $this->getSetting('loop') ? t(', looping') : '' ,
158 '@muted' => $this->getSetting('muted') ? t(', muted') : '',
165 public function viewElements(FieldItemListInterface $items, $langcode) {
167 $files = $this->getEntitiesToView($items, $langcode);
169 // Early opt-out if the field is empty.
174 // Collect cache tags to be added for each item in the field.
175 foreach ($files as $delta => $file) {
176 $video_uri = $file->getFileUri();
177 $elements[$delta] = array(
178 '#theme' => 'video_player_formatter',
179 '#items' => array(Url::fromUri(file_create_url($video_uri))),
180 '#player_attributes' => $this->getSettings(),
189 public static function isApplicable(FieldDefinitionInterface $field_definition) {
190 if(empty($field_definition->getTargetBundle()) && !$field_definition->isList()){
194 $entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default');
195 $widget = $entity_form_display->getRenderer($field_definition->getName());
196 $widget_id = $widget->getBaseId();
197 if(!$field_definition->isList() && $widget_id == 'video_upload'){