5 * Contains \Drupal\videojs\Plugin\Field\FieldFormatter\VideJsPlayerFormatter.
8 namespace Drupal\videojs\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 'videojs_player' formatter.
24 * id = "videojs_player",
25 * label = @Translation("Video.js Player"),
33 class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements ContainerFactoryPluginInterface {
38 * @var \Drupal\Core\Session\AccountInterface
40 protected $currentUser;
43 * Constructs an VideoPlayerFormatter object.
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 * Any third party settings settings.
59 * @param \Drupal\Core\Session\AccountInterface $current_user
62 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user) {
63 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
64 $this->currentUser = $current_user;
70 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
74 $configuration['field_definition'],
75 $configuration['settings'],
76 $configuration['label'],
77 $configuration['view_mode'],
78 $configuration['third_party_settings'],
79 $container->get('current_user')
86 public static function defaultSettings() {
95 ) + parent::defaultSettings();
101 public function settingsForm(array $form, FormStateInterface $form_state) {
102 $element['width'] = [
103 '#title' => t('Width'),
104 '#type' => 'textfield',
105 '#default_value' => $this->getSetting('width'),
108 $element['height'] = [
109 '#title' => t('Height'),
110 '#type' => 'textfield',
111 '#default_value' => $this->getSetting('height'),
114 $element['controls'] = [
115 '#title' => t('Show controls'),
116 '#type' => 'checkbox',
117 '#default_value' => $this->getSetting('controls'),
119 $element['autoplay'] = [
120 '#title' => t('Autoplay'),
121 '#type' => 'checkbox',
122 '#default_value' => $this->getSetting('autoplay'),
125 '#title' => t('Loop'),
126 '#type' => 'checkbox',
127 '#default_value' => $this->getSetting('loop'),
129 $element['muted'] = [
130 '#title' => t('Muted'),
131 '#type' => 'checkbox',
132 '#default_value' => $this->getSetting('muted'),
134 $element['preload'] = [
135 '#title' => t('Preload'),
137 '#default_value' => $this->getSetting('preload'),
140 'metadata' => 'metadata',
143 '#description' => t('Hint to the browser about whether optimistic downloading of the video itself or its metadata is considered worthwhile.')
151 public function settingsSummary() {
153 $summary[] = t('HTML5 Video (@widthx@height@controls@autoplay@loop@muted).', [
154 '@width' => $this->getSetting('width'),
155 '@height' => $this->getSetting('height'),
156 '@controls' => $this->getSetting('controls') ? t(', controls') : '' ,
157 '@autoplay' => $this->getSetting('autoplay') ? t(', autoplaying') : '' ,
158 '@loop' => $this->getSetting('loop') ? t(', looping') : '' ,
159 '@muted' => $this->getSetting('muted') ? t(', muted') : '',
166 public function viewElements(FieldItemListInterface $items, $langcode) {
168 $files = $this->getEntitiesToView($items, $langcode);
170 // Early opt-out if the field is empty.
175 // Collect cache tags to be added for each item in the field.
176 foreach ($files as $delta => $file) {
177 $video_uri = $file->getFileUri();
178 $elements[$delta] = array(
179 '#theme' => 'videojs',
180 '#items' => array(Url::fromUri(file_create_url($video_uri))),
181 '#player_attributes' => $this->getSettings(),
182 '#attached' => array(
183 'library' => array('videojs/videojs'),
193 public static function isApplicable(FieldDefinitionInterface $field_definition) {
194 return !$field_definition->isList();