Using node_load() function im printing out node with jplayer in page.tpl.php:

$node = node_load('100');
node_view($node);
print $node->field_audio[0]['view];

Player loading well, but javascripts which comes with this player not loading when im not viewing a node with jplayer. I mean scripts not including when i am on front page.

/**
 * Add the jPlayer library to the page.
 *
 * @param $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.
 */
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', ''),
    ));
    if ($add) {
      drupal_add_js($filepath);
      drupal_add_js($jplayer_js);
      drupal_add_css($jplayer_css);

    }
    $return = array(
      'js' => array(
        array('data' => $filepath),
        array('data' => $jplayer_js),
        array('data' => $settings, 'type' => 'setting'),
      ),
      'css' => array(
        array('data' => $jplayer_css),
      ),
    );
  }

  return $return;
}

What to fix in this piece of code? To make scripts appear on every page, so the player would work independed - viewing u that node or not.

Comments

deviantintegral’s picture

Status: Active » Fixed

Your problem is that you're doing it within the page template. By that point, the JavaScript has already been generated and included in the page output. You should never be doing "data" (meaning the node_load() call) within a .tpl.php file. Instead, do it in a preprocess function. I'm not sure what your intention is, but my guess is that what you want to do is best done within a module as a custom block. If it's involving data, it belongs in a module, not the theme.

Also, your node_load() call should pass the integer 100, not the string '100'.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.