You are here

public static function JwPlayer::preRenderPlayer in JW Player 8

#pre_render callback for #type 'jw_player'.

Parameters

array $element: An associative array containing the properties and children of the table element.

Return value

array The processed element.

File

src/Element/JwPlayer.php, line 59

Class

JwPlayer
Provides a render element for a table.

Namespace

Drupal\jw_player\Element

Code

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;
}