开发者

jQuery .load not getting filepath variable

开发者 https://www.devze.com 2023-02-18 05:06 出处:网络
So I have a custom Drupal module that outputs a formatted version of the song node\'s data based on a node id in the path (load/song/NID).This function works fine, and when I look at the url in my bro

So I have a custom Drupal module that outputs a formatted version of the song node's data based on a node id in the path (load/song/NID). This function works fine, and when I look at the url in my browser, I see that everything has loaded perfectly.

My module's code:

function load_song($nid){
$node = node_load($nid);
$songname = $node->title;

$albumid = $node->field_album['und'][0]['nid'];
$album = node_load($albumid);
$file = $album->field_cover['und'][0];

//Loads the album filepath from the file array returned above
$filepath = $file['uri'];

//The path returned is something like "public://file.jpg"
$filepath = str_replace("public://", "http://mysite.co开发者_JAVA百科m/sites/default/files/styles/thumbnail/public/", $filepath);

//I then set a variable (imgurl) to the formatted filepath
$imgurl = $filepath;

$artistid = $album->field_artist['und'][0]['nid'];
$artist = node_load($artistid);
$artistname = $artist->title;

echo 'I output the variables + formatting here';
}

With the output, I then load it in my page.tpl.php file in Drupal using the jQuery .load function. My code:

function loadSongInfo(id) {
$("#current-song").html('Loading').load('http://mysite.com/load/song/' + id); 
}

So with this input, what I get is the data from the url (which is perfectly formatted), and the other variables I load (artist name, song name, etc) working fine. However, where I have the filepath, all I get is an empty string.

This of course confuses the hell out of me, because it works fine in my browser, but when I call the function in jQuery, it loads all the other variables fine, except for the filepath.

I've tried (and got unsuccessful results):

  • Casting the variable $filepath to a string using three different methods
  • (I thought it might be something weird with the url) I replaced the filepath variable with just the static string of it ("http://mysite.com/sites/default/files/styles/thumbnail/public/file.jpg"), which returned the correct result, but still fails with the actual variable
  • var_dump
  • print_r

Does anyone have any idea why this isn't working?


Mymodule

function mymodule_menu(){
  $items = array();
  $items['js/song'] = array(
      'page callback' => 'load_song',
      'type' => MENU_CALLBACK,
      'access arguments' => array('access content')
  );
}

function load_song() {
  $nid = $_POST['nid'];
  $node = node_load($nid);

  /*
   * Check for node existing
   * Return response.error if not loaded
   */
  if(!$node->nid){
    drupal_json(array('error' => t('Some text for error')));
    die();
  }

  $songtitle = $node->title;

  $albumid = $node->field_album['und'][0]['nid'];
  $album = node_load($albumid);
  $file = $album->field_cover['und'][0];

  // Loads the album filepath from the file array returned above
  $filepath = $file['uri'];

  // The path returned is something like "public://file.jpg"
  $filepath = str_replace("public://", "http://mysite.com/sites/default/files/styles/thumbnail/public/", $filepath);

  // I then set a variable (imgurl) to the formatted filepath
  $imagepath = $filepath;

  $artistid = $album->field_artist['und'][0]['nid'];
  $artist = node_load($artistid);
  $artistname = $artist->title;

  $object = array(
      'song_title' => l($songtitle, 'node/'. $node->nid),
      'image_path' => $imagepath,
      'artist_name' => l($artistname, $artist->nid)
  );
  drupal_json(array('data' => $object));
  die();
}

Javascript:

Drupal.behaviors.SongInit = function(context){
  $("#current-song").html('Loading...').load("/load/song", {
    nid : id
  }, function(response, status, xhr) {
    if (status == 'error') {
      var msg = Drupal.t("Sorry but there was an error: ");
      $("#current-song").html(msg + xhr.status + " " + xhr.statusText);
    }
    if (response.error) {
      var msg = Drupal.t("Sorry but there was an error: ");
      $("#current-song").html(msg);
    }
    else {
      var msg = response.data.song_title + '<br />';
      var msg = '<img src=/"' + response.data.image_path + ' /><br />';
      var msg = response.data.artist_name + '<br />';
      $("#current-song").html(msg);
      Drupal.attachBehaviors(context);
    }
  });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号