Commit d82ab713 authored by Stefanos Petrakis's avatar Stefanos Petrakis Committed by Max Bronsema
Browse files

Issue #3275879 by stefanos.petrakis: Add support for <track> and vtt files to...

Issue #3275879 by stefanos.petrakis: Add support for <track> and vtt files to the Remote Video entity
parent 36c7a448
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -126,7 +126,7 @@ function ableplayer_install() {
  $bundles = \Drupal::entityTypeManager()->getListBuilder('media_type')->load();

  foreach ($bundles as $bundle) {
    if (!in_array($bundle->get('source'), ['video_file', 'audio_file'])) {
    if (!in_array($bundle->get('source'), ['video_file', 'audio_file', 'oembed:video'])) {
      continue;
    }

+4 −0
Original line number Diff line number Diff line
services:
  ableplayer.video_formatter_helper:
    class: Drupal\ableplayer\AblePlayerVideoFormatterHelper
    arguments: ['@renderer']
+55 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\ableplayer;

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Render\RendererInterface;

/**
 * AblePlayerVideoFormatterHelper service.
 */
class AblePlayerVideoFormatterHelper {

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs an AblePlayerVideoFormatterHelper object.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   */
  public function __construct(RendererInterface $renderer) {
    $this->renderer = $renderer;
  }

  /**
   * Attaches render arrays for fields providing video tracks.
   */
  public function attachTracksFromParentEntity(FieldableEntityInterface $parent, array &$elements) {
    if ($parent->hasField('ableplayer_caption')) {
      foreach ($elements as &$element) {
        $element['caption'] = $parent->ableplayer_caption->view(['type' => 'ableplayer_caption']);
      }
    }

    if ($parent->hasField('ableplayer_chapter')) {
      foreach ($elements as &$element) {
        $element['chapter'] = $parent->ableplayer_chapter->view(['type' => 'ableplayer_chapter']);
      }
    }

    if ($parent->hasField('ableplayer_poster_image')) {
      foreach ($elements as &$element) {
        $build = $parent->ableplayer_poster_image->view(['type' => 'ableplayer_poster_image']);
        $poster = $this->renderer->render($build);
        $element['#attributes']->setAttribute('poster', $poster);
      }
    }
  }

}
+21 −6
Original line number Diff line number Diff line
@@ -2,10 +2,12 @@

namespace Drupal\ableplayer\Plugin\Field\FieldFormatter;

use Drupal\ableplayer\AblePlayerVideoFormatterHelper;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Template\Attribute;
use Drupal\media\OEmbed\UrlResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -32,6 +34,13 @@ class AbleplayerRemoteVideoFormatter extends FormatterBase {
   */
  protected $urlResolver;

  /**
   * The Video Formatter helper.
   *
   * @var \Drupal\ableplayer\AblePlayerVideoFormatterHelper
   */
  protected $helper;

  /**
   * Constructs a FormatterBase object.
   *
@@ -51,11 +60,14 @@ class AbleplayerRemoteVideoFormatter extends FormatterBase {
   *   Any third party settings.
   * @param \Drupal\media\OEmbed\UrlResolverInterface $url_resolver
   *   The oEmbed URL resolver service.
   * @param \Drupal\ableplayer\AblePlayerVideoFormatterHelper $helper
   *   The Video Formatter helper service.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, UrlResolverInterface $url_resolver) {
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, UrlResolverInterface $url_resolver, AblePlayerVideoFormatterHelper $helper) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);

    $this->urlResolver = $url_resolver;
    $this->helper = $helper;
  }

  /**
@@ -70,7 +82,8 @@ class AbleplayerRemoteVideoFormatter extends FormatterBase {
      $configuration['label'],
      $configuration['view_mode'],
      $configuration['third_party_settings'],
      $container->get('media.oembed.url_resolver')
      $container->get('media.oembed.url_resolver'),
      $container->get('ableplayer.video_formatter_helper')
    );
  }

@@ -161,10 +174,10 @@ class AbleplayerRemoteVideoFormatter extends FormatterBase {
        $element[$delta] = [
          '#type' => 'html_tag',
          '#tag' => 'video',
          '#attributes' => [
          '#attributes' => new Attribute([
            'data-able-player' => '',
            'data-youtube-id' => $id,
          ],
          ]),
          '#attached' => [
            'library' => [
              'ableplayer/ableplayer',
@@ -176,10 +189,10 @@ class AbleplayerRemoteVideoFormatter extends FormatterBase {
        $element[$delta] = [
          '#type' => 'html_tag',
          '#tag' => 'video',
          '#attributes' => [
          '#attributes' => new Attribute([
            'data-able-player' => '',
            'data-vimeo-id' => $id,
          ],
          ]),
          '#attached' => [
            'library' => [
              'ableplayer/ableplayer',
@@ -189,6 +202,8 @@ class AbleplayerRemoteVideoFormatter extends FormatterBase {
        ];
      }
    }
    $this->helper->attachTracksFromParentEntity($items->getEntity(), $element);

    return $element;
  }

+53 −20
Original line number Diff line number Diff line
@@ -2,10 +2,12 @@

namespace Drupal\ableplayer\Plugin\Field\FieldFormatter;

use Drupal\ableplayer\AblePlayerVideoFormatterHelper;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;

use Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin implementation of the 'ableplayer_video' formatter.
@@ -21,6 +23,55 @@ use Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase;
 */
class AbleplayerVideoFormatter extends FileMediaFormatterBase {

  /**
   * The Video Formatter helper.
   *
   * @var \Drupal\ableplayer\AblePlayerVideoFormatterHelper
   */
  protected $helper;

  /**
   * Constructs a FormatterBase object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings.
   * @param \Drupal\ableplayer\AblePlayerVideoFormatterHelper $helper
   *   The Video Formatter helper service.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AblePlayerVideoFormatterHelper $helper) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);

    $this->helper = $helper;
  }

  /**
   * {@inheritdoc}
   */
  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('ableplayer.video_formatter_helper')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -45,25 +96,7 @@ class AbleplayerVideoFormatter extends FileMediaFormatterBase {
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = parent::viewElements($items, $langcode);
    $parent = $items->getEntity();

    if ($parent->hasField('ableplayer_caption')) {
      foreach ($elements as &$element) {
        $element['#caption'] = $parent->ableplayer_caption->view(['type' => 'ableplayer_caption']);
      }
    }

    if ($parent->hasField('ableplayer_chapter')) {
      foreach ($elements as &$element) {
        $element['#chapter'] = $parent->ableplayer_chapter->view(['type' => 'ableplayer_chapter']);
      }
    }

    if ($parent->hasField('ableplayer_poster_image')) {
      foreach ($elements as &$element) {
        $poster = render($parent->ableplayer_poster_image->view(['type' => 'ableplayer_poster_image']));
        $element['#attributes']->setAttribute('poster', $poster);
      }
    }
    $this->helper->attachTracksFromParentEntity($parent, $elements);

    return $elements;
  }