Commit 7959f599 authored by Marcos Cano's avatar Marcos Cano 💬 Committed by Marcos Cano
Browse files

Issue #3301795 by Chris Matthews, marcoscano: Add support for Brightcove videos

parent b08a7d5f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ Supported providers / Formatters included:

- Apple Podcasts
- Box
- Brightcove
- Buzzsprout
- Dacast
- Deezer
+8 −0
Original line number Diff line number Diff line
@@ -24,6 +24,14 @@ field.formatter.settings.media_remote_box:
      type: string
      label: 'Iframe height'

field.formatter.settings.media_remote_brightcove:
  type: mapping
  label: 'Brightcove display format settings'
  mapping:
    formatter_class:
      type: string
      label: 'Formatter class name'

field.formatter.settings.media_remote_buzzsprout:
  type: mapping
  label: 'Buzzsprout display format settings'
+5 −0
Original line number Diff line number Diff line
@@ -50,6 +50,11 @@ function media_remote_theme($existing, $type, $theme, $path) {
        'height' => NULL,
      ],
    ],
    'media_remote_brightcove' => [
      'variables' => [
        'url' => NULL,
      ],
    ],
    'media_remote_buzzsprout' => [
      'variables' => [
        'episode_id' => NULL,
+73 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\media_remote\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'media_remote_brightcove' formatter.
 *
 * @FieldFormatter(
 *   id = "media_remote_brightcove",
 *   label = @Translation("Remote Media - Brightcove"),
 *   field_types = {
 *     "string"
 *   }
 * )
 */
class MediaRemoteBrightcoveFormatter extends MediaRemoteFormatterBase {

  /**
   * {@inheritdoc}
   */
  public static function getUrlRegexPattern() {
    return '/^https:\/\/players\.brightcove\.net\/([\d]+)\/default_default\/index\.html\?videoId=([\d]+)/';
  }

  /**
   * {@inheritdoc}
   */
  public static function getValidUrlExampleStrings(): array {
    return [
      'https://players.brightcove.net/[account-id]/default_default/index.html?videoId=[video-id]',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
      /** @var \Drupal\Core\Field\FieldItemInterface $item */
      if ($item->isEmpty()) {
        continue;
      }
      $matches = [];
      $pattern = static::getUrlRegexPattern();
      preg_match_all($pattern, $item->value, $matches);
      if (empty($matches[1][0])) {
        continue;
      }
      $elements[$delta] = [
        '#theme' => 'media_remote_brightcove',
        '#url' => $item->value,
      ];
    }
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public static function deriveMediaDefaultNameFromUrl($url) {
    $pattern = static::getUrlRegexPattern();
    if (preg_match($pattern, $url)) {
      return t('Brightcove video @url', [
        '@url' => $url,
      ]);
    }
    return parent::deriveMediaDefaultNameFromUrl($url);
  }

}
+18 −0
Original line number Diff line number Diff line
{#
/**
 * @file
 * Template implementation for the media_remote_brightcove theme hook.
 *
 * Available variables:
 * - url: (string) The full URL of the remote video.
 */
#}
<div style="position: relative; display: block; max-width: 960px;">
  <div style="padding-top: 56.25%;">
    <iframe
      src="{{ url }}"
      allowfullscreen=""
      allow="encrypted-media"
      style="position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%;"></iframe>
  </div>
</div>