You are here

class JwPlayer in JW Player 8

Provides a render element for a table.

Properties:

  • #file_url: The URL to the file that should be displayed.
  • #preset: (optional) Jw Player preset, if not given, uses default settings.
  • #html_id: (optional) An HTML ID, a random one is generated if not provided.

Usage example:

$build['player'] = array(
  '#type' => 'jw_player',
  '#preset' => 'example',
  '#file_url' => 'public://video.mp4',
  '#html_id' => 'example-id',
);

Plugin annotation

@FormElement("jw_player");

Hierarchy

Expanded class hierarchy of JwPlayer

1 #type use of JwPlayer
JwplayerFormatter::viewElements in src/Plugin/Field/FieldFormatter/JwplayerFormatter.php
Builds a renderable array for a field value.

File

src/Element/JwPlayer.php, line 29

Namespace

Drupal\jw_player\Element
View source
class JwPlayer extends RenderElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#theme' => 'jw_player',
      '#pre_render' => [
        [
          $class,
          'preRenderPlayer',
        ],
      ],
      '#settings' => [],
      '#attached' => [
        'library' => [
          'jw_player/behaviors',
        ],
      ],
    ];
  }

  /**
   * #pre_render callback for #type 'jw_player'.
   *
   * @param array $element
   *   An associative array containing the properties and children of the
   *   table element.
   *
   * @return array
   *   The processed element.
   */
  public static function preRenderPlayer($element) {
    $config = \Drupal::config('jw_player.settings');
    $settings = $element['#settings'];
    if (!isset($element['#html_id'])) {

      // Give each instance of the player a unique id. A random hash is
      // used in place of drupal_html_id() due to potentially conflicting
      // ids in cases where the entire output of the theme function is
      // cached. Prefix with jwplayer, as ID's that start with a number
      // are not valid.
      $element['#html_id'] = 'jwplayer-' . md5(rand());
    }

    // Create a configuration array which will be passed to JWPlayer's
    // JavaScript.
    $settings['file'] = $element['#file_url'];
    if (!empty($element['#preset'])) {
      $preset = Jw_player::load($element['#preset']);

      // Additional check to ensure that the preset has actually loaded. This
      // prevents problems where a preset has been deleted but a field is still
      // configured to use it.
      if (!empty($preset)) {

        // Don't apply preset or default config in case of cloud hosted players.
        if (!$config
          ->get('cloud_player_library_url')) {

          // Merge in the preset settings.
          $settings += $preset
            ->getSettings();
        }
        $cacheability_metadata = CacheableMetadata::createFromRenderArray($element);
        $cacheability_metadata
          ->addCacheableDependency($preset);
        $cacheability_metadata
          ->addCacheableDependency($config);
        $cacheability_metadata
          ->applyTo($element);
      }
    }
    if (!$config
      ->get('cloud_player_library_url')) {
      $settings += static::getDefaultSettings();
      if (isset($settings['mode'])) {
        $settings['primary'] = $settings['mode'];
        unset($settings['mode']);
      }
      if (isset($settings['controlbar']) && !$settings['controlbar']) {
        unset($settings['controlbar']);
      }
    }

    // Unset advertising if is not set.
    if (empty($settings['advertising']['client'])) {
      unset($settings['advertising']);
    }
    else {

      // Add the add break pre roll to schedule if set.
      if (!empty($settings['advertising']['tag'])) {

        // Add the add break pre roll to schedule if set.
        $settings['advertising']['schedule']['adbreak-preroll'] = [
          'tag' => $settings['advertising']['tag'],
          'offset' => 'pre',
        ];
        unset($settings['advertising']['tag']);
      }

      // Add the add break post roll to schedule if set.
      if (!empty($settings['advertising']['tag_post'])) {
        $settings['advertising']['schedule']['adbreak-postroll'] = [
          'tag' => $settings['advertising']['tag_post'],
          'offset' => 'post',
        ];
      }
      unset($settings['advertising']['tag_post']);
    }

    // Unset sharing if it is not set.
    if (isset($settings['sharing']) && $settings['sharing']) {
      unset($settings['sharing']);
      $settings['sharing']['sites'] = [];
      foreach ($settings['sharing_sites']['sites'] as $key => $value) {
        if ($value['enabled'] == 1) {
          $settings['sharing']['sites'][] = $key;
        }
      }

      // If none selected, all selected.
      if (!$settings['sharing']['sites']) {
        foreach ($settings['sharing_sites']['sites'] as $key => $value) {
          $settings['sharing']['sites'][] = $key;
        }
      }
      $settings['sharing']['heading'] = $settings['sharing_heading'];
    }
    unset($settings['sharing_sites']);
    unset($settings['sharing_heading']);

    // Add the build settings to drupal settings.
    $element['#attached']['drupalSettings']['jw_player']['players'][$element['#html_id']] = $settings;

    // Add the license_key if provided.
    if ($config
      ->get('jw_player_key')) {
      $element['#attached']['drupalSettings']['jw_player']['license_key'] = $config
        ->get('jw_player_key');
    }
    return $element;
  }

  /**
   * Default JW PLayer settings.
   *
   * @return array
   *    Returns the default settings for JW Player. Used in cases where a preset
   *    is not provided when the JW Player theme function is called.
   */
  public static function getDefaultSettings() {
    $defaults = array(
      'width' => '640',
      'height' => '480',
      'mode' => 'html5',
      'autostart' => FALSE,
      'controlbar' => 'bottom',
      'advertising' => array(
        'client' => '',
        'tag' => '',
      ),
    );
    $library_discovery = \Drupal::service('library.discovery');
    $library = $library_discovery
      ->getLibraryByName('jw_player', 'jwplayer');
    if (!empty($library['library path'])) {
      $defaults['base'] = file_create_url($library['library path'] . '/');

      // JW Player 7+ no longer uses the base for the flash path but supports
      // an explicit configuration option for it again.
      $defaults['flashplayer'] = $defaults['base'] . 'jwplayer.flash.swf';
    }
    return $defaults;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
JwPlayer::getDefaultSettings public static function Default JW PLayer settings.
JwPlayer::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
JwPlayer::preRenderPlayer public static function #pre_render callback for #type 'jw_player'.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.