View source
<?php
namespace Drupal\video\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
class VideoPlayerFormatter extends VideoPlayerFormatterBase implements ContainerFactoryPluginInterface {
protected $currentUser;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('current_user'));
}
public static function defaultSettings() {
return [
'width' => '854',
'height' => '480',
'controls' => TRUE,
'autoplay' => FALSE,
'loop' => FALSE,
'muted' => FALSE,
'preload' => 'none',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['width'] = [
'#title' => t('Width'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('width'),
'#required' => TRUE,
];
$element['height'] = [
'#title' => t('Height'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('height'),
'#required' => TRUE,
];
$element['controls'] = [
'#title' => t('Show controls'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('controls'),
];
$element['autoplay'] = [
'#title' => t('Autoplay'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('autoplay'),
];
$element['loop'] = [
'#title' => t('Loop'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('loop'),
];
$element['muted'] = [
'#title' => t('Muted'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('muted'),
];
$element['preload'] = [
'#title' => t('Preload'),
'#type' => 'select',
'#default_value' => $this
->getSetting('preload'),
'#options' => [
'none' => 'none',
'metadata' => 'metadata',
'auto' => 'auto',
],
'#description' => t('Hint to the browser about whether optimistic downloading of the video itself or its metadata is considered worthwhile.'),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$summary[] = t('HTML5 Video (@widthx@height@controls@autoplay@loop@muted).', [
'@width' => $this
->getSetting('width'),
'@height' => $this
->getSetting('height'),
'@controls' => $this
->getSetting('controls') ? t(', controls') : '',
'@autoplay' => $this
->getSetting('autoplay') ? t(', autoplaying') : '',
'@loop' => $this
->getSetting('loop') ? t(', looping') : '',
'@muted' => $this
->getSetting('muted') ? t(', muted') : '',
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$files = $this
->getEntitiesToView($items, $langcode);
if (empty($files)) {
return $elements;
}
foreach ($files as $delta => $file) {
$video_uri = $file
->getFileUri();
$relative_url = file_url_transform_relative(file_create_url($video_uri));
$elements[$delta] = [
'#theme' => 'video_player_formatter',
'#items' => [
Url::fromUserInput($relative_url),
],
'#player_attributes' => $this
->getSettings(),
];
}
return $elements;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
if (empty($field_definition
->getTargetBundle())) {
return TRUE;
}
else {
$entity_form_display = entity_get_form_display($field_definition
->getTargetEntityTypeId(), $field_definition
->getTargetBundle(), 'default');
$widget = $entity_form_display
->getRenderer($field_definition
->getName());
$widget_id = $widget
->getBaseId();
if ($widget_id == 'video_upload') {
return TRUE;
}
}
return FALSE;
}
}