You are here

function jplayer_add in jPlayer 6

Add the jPlayer library to the page.

Parameters

$add: By default this function will add jPlayer to the page JavaScript array directly. If wanting to store the jPlayer file as an #attached property, set this to FALSE and jplayer_add() will only return the needed array suitable for use as an #attached property.

3 calls to jplayer_add()
template_preprocess_jplayer_view_playlist in includes/jplayer.theme.inc
Preprocess function for jplayer.tpl.php when displaying a view as a playlist.
theme_jplayer_formatter_playlist in includes/jplayer.theme.inc
Themes multiple items as playlist.
theme_jplayer_formatter_single in includes/jplayer.theme.inc
Themes single file.

File

./jplayer.module, line 99
Provides an HTML5-compatible with Flash-fallback audio player.

Code

function jplayer_add($add = TRUE) {
  static $added = FALSE;
  $directory = variable_get('jplayer_directory', 'sites/all/libraries/jplayer');
  $return = FALSE;
  if (file_exists($directory . '/jquery.jplayer.min.js')) {
    $filepath = $directory . '/jquery.jplayer.min.js';
  }
  elseif (file_exists($directory . '/jquery.jplayer.js')) {
    $filepath = $directory . '/jquery.jplayer.js';
  }
  if (isset($filepath)) {
    $jplayer_js = jplayer_get_file_path('jplayer.js');
    $jplayer_css = jplayer_get_file_path('jplayer.css');
    $settings = array(
      'jPlayer' => array(
        'swfPath' => base_path() . variable_get('jplayer_directory', 'sites/all/libraries/jplayer'),
        'autoPlay' => (int) variable_get('jplayer_autoplay', ''),
        'pauseOthers' => variable_get('jplayer_pause_others', FALSE),
        'protected' => variable_get('jplayer_protected', ''),
      ),
    );
    if ($add) {
      drupal_add_js($filepath);
      drupal_add_js($jplayer_js);
      drupal_add_css($jplayer_css);
      if (!$added) {
        drupal_add_js($settings, 'setting');
        $added = TRUE;
      }
    }
    $return = array(
      'js' => array(
        array(
          'data' => $filepath,
        ),
        array(
          'data' => $jplayer_js,
        ),
        array(
          'data' => $settings,
          'type' => 'setting',
        ),
      ),
      'css' => array(
        array(
          'data' => $jplayer_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;
    if ($add) {
      foreach ($additional_resources as $resource) {
        $js_info = $resource['#attached']['js'];
        foreach ($js_info as $js_path => $js_parameters) {
          drupal_add_js($js_path, $js_parameters['type'], $js_parameters['scope']);
        }
      }
    }
  }
  return $return;
}