View source
<?php
namespace Drupal\video\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\video\ProviderManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\file\Entity\File;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Field\FieldDefinitionInterface;
class VideoEmbedPlayerFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $providerManager;
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$settings = $this
->getSettings();
foreach ($items as $delta => $item) {
$file = File::load($item->target_id);
if (!$file) {
continue;
}
$metadata = isset($item->data) ? unserialize($item->data) : [];
$scheme = file_uri_scheme($file
->getFileUri());
$provider = $this->providerManager
->loadProviderFromStream($scheme, $file, $metadata);
if ($provider) {
$element[$delta] = $provider
->renderEmbedCode($settings);
}
}
return $element;
}
public static function defaultSettings() {
return [
'width' => '854',
'height' => '480',
'autoplay' => TRUE,
'related_videos' => FALSE,
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = [];
$form['autoplay'] = [
'#title' => t('Autoplay'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('autoplay'),
];
$form['related_videos'] = [
'#title' => t('Related Videos'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('related_videos'),
];
$form['width'] = [
'#title' => t('Width'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('width'),
'#required' => TRUE,
];
$form['height'] = [
'#title' => t('Height'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('height'),
'#required' => TRUE,
];
return $form;
}
public function settingsSummary() {
$summary[] = t('Embedded Video (@widthx@height@autoplay@related_videos).', [
'@width' => $this
->getSetting('width'),
'@height' => $this
->getSetting('height'),
'@related_videos' => $this
->getSetting('related_videos') ? t(', showing related videos') : '',
'@autoplay' => $this
->getSetting('autoplay') ? t(', autoplaying') : '',
]);
return $summary;
}
public function __construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, ProviderManagerInterface $provider_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->providerManager = $provider_manager;
}
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('video.provider_manager'));
}
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_embed') {
return TRUE;
}
}
return FALSE;
}
}