5 * Contains \Drupal\video\Plugin\Field\FieldFormatter\VideoEmbedPlayerFormatter.
8 namespace Drupal\video\Plugin\Field\FieldFormatter;
10 use Drupal\Core\Field\FormatterBase;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14 use Drupal\video\ProviderManagerInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Drupal\file\Entity\File;
17 use Drupal\Core\File\FileSystem;
18 use Drupal\Core\Field\FieldDefinitionInterface;
21 * Plugin implementation of the video field formatter.
24 * id = "video_embed_player",
25 * label = @Translation("Embedded Video Player"),
31 class VideoEmbedPlayerFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
34 * The embed provider plugin manager.
36 * @var \Drupal\video\ProviderManagerInterface
38 protected $providerManager;
43 public function viewElements(FieldItemListInterface $items, $langcode) {
45 $settings = $this->getSettings();
46 foreach ($items as $delta => $item) {
47 $file = File::load($item->target_id);
49 $metadata = isset($item->data) ? unserialize($item->data) : array();
50 $scheme = file_uri_scheme($file->getFileUri());
51 $provider = $this->providerManager->loadProviderFromStream($scheme, $file, $metadata);
53 $element[$delta] = $provider->renderEmbedCode($settings);
62 public static function defaultSettings() {
73 public function settingsForm(array $form, FormStateInterface $form_state) {
75 '#title' => t('Autoplay'),
76 '#type' => 'checkbox',
77 '#default_value' => $this->getSetting('autoplay'),
80 '#title' => t('Width'),
81 '#type' => 'textfield',
82 '#default_value' => $this->getSetting('width'),
86 '#title' => t('Height'),
87 '#type' => 'textfield',
88 '#default_value' => $this->getSetting('height'),
97 public function settingsSummary() {
98 $summary[] = t('Embedded Video (@widthx@height@autoplay).', [
99 '@width' => $this->getSetting('width'),
100 '@height' => $this->getSetting('height'),
101 '@autoplay' => $this->getSetting('autoplay') ? t(', autoplaying') : '' ,
107 * Constructs a new instance of the plugin.
109 * @param string $plugin_id
110 * The plugin_id for the formatter.
111 * @param mixed $plugin_definition
112 * The plugin implementation definition.
113 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
114 * The definition of the field to which the formatter is associated.
115 * @param array $settings
116 * The formatter settings.
117 * @param string $label
118 * The formatter label display setting.
119 * @param string $view_mode
121 * @param array $third_party_settings
122 * Third party settings.
123 * @param \Drupal\video\ProviderManagerInterface $provider_manager
124 * The video embed provider manager.
126 public function __construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, ProviderManagerInterface $provider_manager) {
127 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
128 $this->providerManager = $provider_manager;
134 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
138 $configuration['field_definition'],
139 $configuration['settings'],
140 $configuration['label'],
141 $configuration['view_mode'],
142 $configuration['third_party_settings'],
143 $container->get('video.provider_manager')
150 public static function isApplicable(FieldDefinitionInterface $field_definition) {
151 if(empty($field_definition->getTargetBundle())){
155 $entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default');
156 $widget = $entity_form_display->getRenderer($field_definition->getName());
157 $widget_id = $widget->getBaseId();
158 if($widget_id == 'video_embed'){