jw_player.module in JW Player 8
Same filename and directory in other branches
Adds a theme function which allows theme developers to use the JW Player.
File
jw_player.moduleView source
<?php
/**
* @file
* Adds a theme function which allows theme developers to use the JW Player.
*/
use Drupal\Core\Render\Element;
use Drupal\jw_player\Entity\Jw_player;
/**
* Implements hook_theme().
*/
function jw_player_theme() {
return array(
'jw_player' => array(
'variables' => array(
'preset' => '',
'file_url' => '',
'file_mime' => '',
'file' => NULL,
'item' => NULL,
'options' => array(),
'html_id' => '',
),
'template' => 'jw_player',
),
);
}
/**
* Retrieves all available preset plugins.
*/
function jw_player_preset_plugins($name = NULL) {
$plugins =& drupal_static(__FUNCTION__);
if (!isset($plugins)) {
$plugins = \Drupal::moduleHandler()
->invokeAll('jw_player_plugin_info');
// Allow modules to alter other modules' plugin definitions.
\Drupal::moduleHandler()
->alter('jw_player_plugin_info', $plugins);
}
if ($name && isset($plugins[$name])) {
return $plugins[$name];
}
return $plugins;
}
/**
* Retrieves information about JW Player skins.
*
* @param string $name
* Name of skin for which information will be returned (optional).
*
* @return object|array
* If $name is provided, will return the $file object. Otherwise if $name
* is NULL, will return an array of $file objects.
*/
function jw_player_skins($name = NULL) {
$skins =& drupal_static(__FUNCTION__);
$config = \Drupal::config('jw_player.settings');
if (!isset($skins)) {
$skins = [];
// Get custom JW Player skins stored in 'jwplayer_skins' directory.
$pattern = jw_player_use_legacy() ? '/\\.xml|\\.swf$/' : '/\\.css$/';
if (\Drupal::hasService('library.libraries_directory_file_finder')) {
/** @var \Drupal\Core\Asset\LibrariesDirectoryFileFinder $library_file_finder */
$library_file_finder = \Drupal::service('library.libraries_directory_file_finder');
$directory = $library_file_finder
->find('jwplayer_skins');
}
elseif (\Drupal::moduleHandler()
->moduleExists('libraries')) {
$directory = libraries_get_path('jwplayer_skins');
}
else {
$directory = 'libraries/jwplayer_skins';
}
$custom_skins = [];
if ($directory && !file_exists($directory)) {
$custom_skins = \Drupal::service('file_system')
->scanDirectory($directory, $pattern);
foreach ($custom_skins as $key => $custom_skin) {
$custom_skins[$key]->skin_type = 'custom';
}
}
// Get JW Player 7 skins provided with self-hosted version.
$library_skins = [];
if (!jw_player_use_legacy() && $config
->get('jw_player_key')) {
if (\Drupal::hasService('library.libraries_directory_file_finder')) {
/** @var \Drupal\Core\Asset\LibrariesDirectoryFileFinder $library_file_finder */
$library_file_finder = \Drupal::service('library.libraries_directory_file_finder');
$directory = $library_file_finder
->find('jwplayer');
}
elseif (\Drupal::moduleHandler()
->moduleExists('libraries')) {
$directory = libraries_get_path('jwplayer');
}
else {
$directory = 'libraries/jwplayer_skins';
}
if ($directory && !file_exists($directory)) {
$library_skins = \Drupal::service('file_system')
->scanDirectory($directory . '/skins', $pattern);
foreach ($library_skins as $key => $library_skin) {
$library_skins[$key]->skin_type = 'library';
}
}
}
$skins = array_merge($library_skins, $custom_skins);
}
if ($name) {
foreach ($skins as $file) {
if ($file->name == $name) {
return $file;
}
}
}
// Sort skin array in alphabetical order.
asort($skins);
return $skins;
}
/**
* Implements hook_libraries_info().
*/
function jw_player_libraries_info() {
$libraries = array(
'jwplayer' => array(
'name' => 'JW Player',
'vendor url' => 'http://www.jwplayer.com/',
'download url' => 'http://www.jwplayer.com/download',
'files' => array(
'js' => array(
'jwplayer.js' => array(),
),
),
),
);
// Parse version from JS file for older versions.
if (jw_player_use_legacy()) {
$libraries['jwplayer']['version arguments'] = array(
'file' => 'jwplayer.js',
'pattern' => '/jwplayer.version="(.*?)"/',
'lines' => 1,
);
}
else {
// Version of JWPlayer 7 and above can't be parsed the same way anymore.
$libraries['jwplayer']['version'] = 7;
}
return $libraries;
}
/**
* Implements hook_library_info_alter().
*/
function jw_player_library_info_alter(&$libraries, $extension) {
if ($extension != 'jw_player') {
return;
}
$config = \Drupal::config('jw_player.settings');
if ($cloud_hosted_default = $config
->get('cloud_player_library_url')) {
// Cloud hosted player, use external JavaScript
$libraries['jwplayer']['version'] = 'cloud-hosted';
$libraries['jwplayer']['js'] = [
$cloud_hosted_default => [
'type' => 'external',
],
];
return;
}
// If version 7 is selected, look for a separate jwplayer7 library
// for this. Prefer that if it exists. This allows to have two versions
// of JW Player on the site, one for each major version.
$version_7_path = NULL;
if ($config
->get('jw_player_version') == '7' && $config
->get('jw_player_key')) {
// Check whether the library version 7 exists in library folder.
if (\Drupal::hasService('library.libraries_directory_file_finder')) {
/** @var \Drupal\Core\Asset\LibrariesDirectoryFileFinder $library_file_finder */
$library_file_finder = \Drupal::service('library.libraries_directory_file_finder');
$version_7_path = $library_file_finder
->find('jwplayer7');
}
elseif (\Drupal::moduleHandler()
->moduleExists('libraries')) {
$version_7_path = libraries_get_path('jwplayer7');
}
else {
$version_7_path = 'libraries/jwplayer7';
}
}
if (\Drupal::moduleHandler()
->moduleExists('libraries')) {
$info = libraries_detect('jwplayer');
$libraries['jwplayer'] += array(
'website' => $info['vendor url'],
'version' => $info['installed'] ? $info['version'] : 'cloud-hosted',
);
if ($version_7_path && file_exists($version_7_path)) {
$info['library path'] = $version_7_path;
// Detect full version string for version 7.
$info['version arguments']['pattern'] = '/return"(7\\.[0-9][0-9]?\\.[0-9][0-9]?)/';
// First 100kb should be more then enough to reach version string for
// version 7, which is positioned in first half of the file.
$info['version arguments']['lines'] = 2;
$info['version arguments']['cols'] = 50000;
$info['version'] = libraries_get_version($info, $info['version arguments']);
$info['installed'] = TRUE;
}
if ($info['installed']) {
$libraries['jwplayer']['library path'] = $info['library path'];
// Self hosted player, use files from library definition.
$libraries['jwplayer']['js'] = [];
if (!empty($info['files']['js'])) {
foreach ($info['files']['js'] as $data => $option) {
if (is_numeric($data)) {
$option = "/{$info['library path']}/{$option}";
}
elseif (empty($option['type']) || $option['type'] == 'file') {
$data = "/{$info['library path']}/{$data}";
}
$libraries['jwplayer']['js'][$data] = $option;
}
}
}
// Use integration files from library definition.
foreach ($info['integration files'] as $module => $files) {
foreach (array_keys($files) as $type) {
$module_path = drupal_get_path('module', $module);
foreach ($files[$type] as $data => $option) {
if (is_numeric($data)) {
$option = "{$module_path}/{$option}";
}
elseif (empty($option['type']) || $option['type'] == 'file') {
$data = "{$module_path}/{$data}";
}
$libraries['jwplayer'][$type][$data] = $option;
}
}
}
}
elseif (\Drupal::hasService('library.libraries_directory_file_finder')) {
if ($version_7_path && file_exists($version_7_path)) {
$libraries['jwplayer']['js'] = [
'/' . $version_7_path . '/jwplayer.js' => [
'type' => 'external',
],
];
}
}
}
/**
* Checks whether a legacy version is configured.
*
* @return bool
* TRUE if any version older than 7 is used, FALSE otherwise.
*/
function jw_player_use_legacy() {
$config = \Drupal::config('jw_player.settings');
return $config
->get('jw_player_version') < 7;
}
/**
* Gets the correct key for the corresponding JW Player version.
*
* @return string
* The license key as entered in the UI.
*/
function jw_player_get_key() {
$config = \Drupal::config('jw_player.settings');
return $config
->get('jw_player_key') ? $config
->get('jw_player_key') : NULL;
}
/**
* Helper function to retrieve available JW Player sharing sites.
*
* @return array
* Array of sharing site keys and formatted values.
*/
function jw_player_sharing_sites() {
return [
'facebook' => t('Facebook'),
'twitter' => t('Twitter'),
'pinterest' => t('Pinterest'),
'email' => t('Email'),
'tumblr' => t('Tumblr'),
'googleplus' => t('Google Plus'),
'reddit' => t('Reddit'),
'linkedin' => t('LinkedIn'),
];
}
/**
* Return regex to check JW Player library URL format.
*
* @return string
* The regex string as defined in the function.
*/
function jw_player_library_url_regex() {
return '/^(https?:|)?\\/\\/content\\.jwplatform\\.com\\/libraries\\/([a-zA-Z0-9]*)\\.js$/i';
}
Functions
Name | Description |
---|---|
jw_player_get_key | Gets the correct key for the corresponding JW Player version. |
jw_player_libraries_info | Implements hook_libraries_info(). |
jw_player_library_info_alter | Implements hook_library_info_alter(). |
jw_player_library_url_regex | Return regex to check JW Player library URL format. |
jw_player_preset_plugins | Retrieves all available preset plugins. |
jw_player_sharing_sites | Helper function to retrieve available JW Player sharing sites. |
jw_player_skins | Retrieves information about JW Player skins. |
jw_player_theme | Implements hook_theme(). |
jw_player_use_legacy | Checks whether a legacy version is configured. |