jplayer.module in jPlayer 7.2
Same filename and directory in other branches
Provides an HTML5-compatible with Flash-fallback audio player.
This module provides functionality for loading the jPlayer library and formatters for File Fields.
File
jplayer.moduleView source
<?php
/**
* @file
* Provides an HTML5-compatible with Flash-fallback audio player.
*
* This module provides functionality for loading the jPlayer library and
* formatters for File Fields.
*/
/**
* Implements hook_menu().
*/
function jplayer_menu() {
$items = array();
$items['admin/config/media/jplayer'] = array(
'title' => 'jPlayer',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'jplayer_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'description' => 'Configure the settings for the jPlayer module.',
'file' => 'includes/jplayer.admin.inc',
);
return $items;
}
/**
* Implements hook_theme().
*/
function jplayer_theme() {
return array(
'jplayer' => array(
'template' => 'theme/jplayer',
'variables' => array(
'entity_type' => NULL,
'entity' => NULL,
'field_name' => NULL,
'items' => array(),
'settings' => array(),
'type' => '',
'playlist' => '',
),
'file' => 'includes/jplayer.theme.inc',
),
'jplayer_item_list' => array(
'render element' => 'element',
'file' => 'includes/jplayer.theme.inc',
'variables' => array(
'items' => array(),
'title' => NULL,
'type' => 'ul',
'attributes' => array(),
),
),
'jplayer_view_playlist' => array(
'template' => 'theme/jplayer',
'arguments' => array(
'view' => NULL,
'items' => NULL,
),
'file' => 'includes/jplayer.theme.inc',
),
);
}
/**
* Implements hook_views_api().
*/
function jplayer_views_api() {
return array(
'path' => drupal_get_path('module', 'jplayer') . '/includes',
'api' => 3.0,
);
}
/**
* Implements hook_library().
*/
function jplayer_library() {
$file = _jplayer_get_js();
$version = jplayer_get_version($file);
$libraries = array();
$libraries['jplayer'] = array(
'title' => 'jPlayer',
'website' => 'http://www.jplayer.org',
'version' => $version,
'js' => array(
$file => array(),
),
);
return $libraries;
}
/**
* Implements hook_field_formatter_info().
*/
function jplayer_field_formatter_info() {
return array(
'jplayer_player' => array(
'label' => t('jPlayer - Player'),
'field types' => array(
'file',
'text',
'link_field',
),
'description' => t('Display file fields as an HTML5-compatible with Flash-fallback media player.'),
'settings' => array(
'autoplay' => 0,
'continuous' => 0,
'solution' => 'html, flash',
'preload' => 'metadata',
'volume' => 80,
'muted' => FALSE,
'repeat' => 'none',
'backgroundColor' => '000000',
'mode' => 'playlist',
),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function jplayer_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'jplayer_player':
if (!empty($items)) {
$output = theme('jplayer', array(
'entity_type' => $entity_type,
'entity' => $entity,
'field_name' => $instance['field_name'],
'items' => $items,
'settings' => $display['settings'],
));
}
else {
return array();
}
$element[0] = array(
'#markup' => $output,
);
break;
}
return $element;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function jplayer_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$form = array();
if ($display['type'] == 'jplayer_player') {
$form['mode'] = array(
'#title' => t('Kind'),
'#type' => 'select',
'#options' => array(
'single' => t('Single'),
'playlist' => t('Playlist'),
),
'#default_value' => $settings['mode'],
);
$form['continuous'] = array(
'#title' => t('Continuous play'),
'#type' => 'select',
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('Play the next track in a playlist automatically.'),
'#default_value' => $settings['continuous'],
'#states' => array(
'visible' => array(
':input[name="fields[field_audio][settings_edit_form][settings][mode]"]' => array(
'value' => 'playlist',
),
),
),
);
$form['autoplay'] = array(
'#title' => t('Autoplay'),
'#type' => 'select',
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#default_value' => $settings['autoplay'],
);
$form['solution'] = array(
'#title' => t('Preferred solution'),
'#type' => 'select',
'#options' => array(
'html, flash' => t('HTML5'),
'flash, html' => t('Flash'),
),
'#default_value' => $settings['solution'],
);
$form['preload'] = array(
'#title' => t('Preload media'),
'#type' => 'select',
'#options' => array(
'none' => t('No'),
'metadata' => t('Only metadata'),
'auto' => t('Yes'),
),
'#description' => t("Preloading media before it's requested isn't available in all browsers."),
'#default_value' => $settings['preload'],
);
$form['volume'] = array(
'#title' => t('Initial volume'),
'#type' => 'textfield',
'#field_suffix' => '%',
'#maxlength' => 3,
'#size' => 3,
'#default_value' => $settings['volume'],
'#element_validate' => array(
'jplayer_volume_check',
),
);
$form['muted'] = array(
'#title' => t('Initially muted'),
'#type' => 'select',
'#options' => array(
FALSE => t('No'),
TRUE => t('Yes'),
),
'#default_value' => $settings['muted'],
);
$form['repeat'] = array(
'#title' => t('Repeat'),
'#type' => 'select',
'#options' => array(
'all' => t('All'),
'single' => t('Single'),
'none' => t('None'),
),
'#description' => t("For playlist players, it is suggested to enable Continuous play if setting Repeat to 'All'."),
'#default_value' => $settings['repeat'],
);
$form['backgroundColor'] = array(
'#title' => t('Background color'),
'#type' => 'textfield',
'#field_preffix' => '#',
'#maxlength' => 6,
'#size' => 6,
'#default_value' => $settings['backgroundColor'],
);
}
return $form;
}
/**
* Element validation to check volume is between 0 and 100.
*/
function jplayer_volume_check($element, &$form_state) {
$value = $element['#value'];
if ($value < 0 || $value > 100) {
form_set_error($element, t('Volume must be a percentage between 0% and 100%.'));
}
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function jplayer_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] == 'jplayer_player') {
$summary = t('Single jPlayer instance');
if ($settings['mode'] == 'playlist') {
$summary = t('jPlayer Playlist');
if (isset($settings['continuous']) && $settings['continuous'] == 1) {
$summary .= t('(continuous)');
}
}
if (isset($settings['autoplay']) && $settings['autoplay'] == 1) {
$summary .= t('(autoplays)');
}
$summary .= '.';
}
return $summary;
}
/**
* Return the version of jPlayer installed.
*
* @param string $file
* File path of jPlayer file.
*
* @return string|bool
* Version detected or FALSE.
*/
function jplayer_get_version($file = '') {
if (!$file) {
$file = _jplayer_get_js();
}
$style = 'full';
if (preg_match('/min/', $file)) {
$style = 'min';
}
$contents = file_get_contents($file);
// No file data. Run away.
if (!$contents) {
return FALSE;
}
$matches = array();
$preg = '/Version:?[ ]*([\\d.]+)/i';
if ($style == 'min') {
$preg = '/jPlayer ([\\d.]+) for jQuery/';
}
preg_match($preg, $contents, $matches);
if (isset($matches[1])) {
return $matches[1];
}
return FALSE;
}
/**
* Add core JS and CSS needed for jPlayer instances.
*
* @return array
* Form API attach.
*/
function _jplayer_js_attach() {
$added =& drupal_static(__FUNCTION__, FALSE);
if ($added) {
// JS is already loaded.
return array();
}
// Add global settings.
$settings = array(
'jPlayer' => array(
'swfPath' => _jplayer_get_swf(),
'showHour' => (bool) variable_get('jplayer_showHour', FALSE),
'showMin' => (bool) variable_get('jplayer_showMin', TRUE),
'showSec' => (bool) variable_get('jplayer_showSec', TRUE),
'padHour' => (bool) variable_get('jplayer_padHour', FALSE),
'padMin' => (bool) variable_get('jplayer_padMin', TRUE),
'padSec' => (bool) variable_get('jplayer_padSec', TRUE),
'sepHour' => variable_get('jplayer_sepHour', ':'),
'sepMin' => variable_get('jplayer_sepMin', ':'),
'sepSec' => variable_get('jplayer_sepSec', ''),
),
);
drupal_add_js($settings, array(
'type' => 'setting',
));
$path = drupal_get_path('module', 'jplayer');
$return = array(
'#attached' => array(
// Base library.
'library' => array(
array(
'jplayer',
'jplayer',
),
),
// Drupal specific JS.
'js' => array(
$path . '/theme/jplayer.js' => array(
'type' => 'file',
'scope' => 'footer',
'group' => JS_DEFAULT,
),
),
// CSS of skin.
'css' => _jplayer_get_css(),
),
);
// Allow other modules to add resources to the page when a jPlayer is
// embedded.
if ($additional_resources = module_invoke_all('jplayer_add_js')) {
$return['additional_resources'] = $additional_resources;
}
$added = TRUE;
return $return;
}
/**
* Return a CSS array for the theme.
*
* So far only one skin is acceptable because the HTML is different.
* Keeping it for the future..
*
* "See your future, be your future. May. make. make it. Make your future Danny"
*
* @return array
* CSS array.
*/
function _jplayer_get_css() {
$return = array();
$opts = array(
'type' => 'file',
'group' => CSS_DEFAULT,
);
// Right now only the default skin (module) is avaialble.
$skin = NULL;
if (!$skin) {
$path = drupal_get_path('module', 'jplayer');
return array(
$path . '/theme/jplayer.css' => $opts,
);
}
else {
$lib = libraries_get_path('jplayer') . '/skin/' . $skin;
$file = $lib . '/' . $skin . '.css';
if (file_exists($file)) {
$return = array(
$file => $opts,
);
}
else {
$skin = 'jplayer.' . $skin;
$file = $lib . '/' . $skin . '.css';
if (file_exists($file)) {
$return = array(
$file => $opts,
);
}
}
}
return $return;
}
/**
* Finds the JS file available in the libraries directory.
*
* @return string|bool
* Full file path or FALSE on failure.
*/
function _jplayer_get_js() {
$path = libraries_get_path('jplayer');
$player_files = array(
'min' => '/jquery.jplayer.min.js',
'full' => '/jquery.jplayer.js',
);
// Check main library directory.
foreach ($player_files as $type => $file) {
$return = $path . $file;
if (file_exists($return)) {
return $return;
}
}
// If they just unzipped the dl, the file is in the dist/jplayer folder.
$path .= '/dist/jplayer';
foreach ($player_files as $file) {
$return = $path . $file;
if (file_exists($return)) {
return $return;
}
}
return FALSE;
}
/**
* Locates SWF file.
*
* @return string
* SWF file path.
*/
function _jplayer_get_swf() {
$path = libraries_get_path('jplayer');
$file = '/jquery.jplayer.swf';
$full = '/' . $path . $file;
if (!file_exists($full)) {
$path .= '/dist/jplayer';
$dist = $path . $file;
if (file_exists($dist)) {
return $dist;
}
}
return $full;
}
Functions
Name | Description |
---|---|
jplayer_field_formatter_info | Implements hook_field_formatter_info(). |
jplayer_field_formatter_settings_form | Implements hook_field_formatter_settings_form(). |
jplayer_field_formatter_settings_summary | Implements hook_field_formatter_settings_summary(). |
jplayer_field_formatter_view | Implements hook_field_formatter_view(). |
jplayer_get_version | Return the version of jPlayer installed. |
jplayer_library | Implements hook_library(). |
jplayer_menu | Implements hook_menu(). |
jplayer_theme | Implements hook_theme(). |
jplayer_views_api | Implements hook_views_api(). |
jplayer_volume_check | Element validation to check volume is between 0 and 100. |
_jplayer_get_css | Return a CSS array for the theme. |
_jplayer_get_js | Finds the JS file available in the libraries directory. |
_jplayer_get_swf | Locates SWF file. |
_jplayer_js_attach | Add core JS and CSS needed for jPlayer instances. |