By erdubya on
I have a template that I want to display nodes with but I need to be able to retrieve nodes by a unique identifier (songID).
Say for instance I want to retrieve "Thriller" by Michael Jackson. That song node has a field called songID with the value 29581. The URL for that song node is http://mysite.com/music/thriller_by_michael-jackson/
How can I use arguments in the URL to retrieve the node in my template? i.e.
http://mysite.com/custom-song-template.tpl.php?songID=29581
to point to http://mysite.com/music/thriller_by_michael-jackson/?
OR is there another way I need to retrieve it?
Comments
Which template?
Which template? node.tpl.php already has $node and if you are using path aliases (pathauto would help here) the node id is still available. So exactly what are you trying to do?
It's a custom template that
It's a custom template that will just display the node... but it needs to dynamically display the node based on the field 'songID' coming from the URL.
I'm trying to display different nodes of the content type 'song' based on the songID field/variable is in the URL for the template. This songID exists for every song node and I'm trying to retrieve them dynamically using a URL similar to this:
http://mysite.com/custom-song-template.tpl.php?songID=29581
"http://mysite.com/custom-son
"http://mysite.com/custom-song-template.tpl.php?songID=29581" is not a normal Drupal path, template files are called through theming so it is not clear what you are doing or what is the context.
I see.. so what would be the
I see.. so what would be the normal path to be able to pass in the songID to the template?
I have an external system that has songID as the key for a music database full of song records. These song records have been properly imported into drupal as song nodes, keyed by node ID (of course) but containing the songID field as a cck field.
What i'm trying to build is a bridge of sorts to take the songID from my external system and display the song node with that songID in drupal.
When I thought of the URL I was thinking more in terms of PHP get variables. What would be the best way to accomplish this in drupal?
What drives the need for
What drives the need for displaying by songID? Are you using that to get the data in real time?
If yes, make a template called node-song.tpl.php (assuming the type of a song node is 'song'), in you will have access to $node->field_songID. This template will be used when displaying content of type 'song'.
songID is the key between the
songID is the key between the external system and drupal. Yes I'm getting the data in real time, every 3 or 4 minutes or so (when a song changes).
Ahh I see. so in the template file name node-song.tpl.php does drupal look at the 'node-song' part of the name, or can it be anything? If the template is only called by theming, then it seems as if my method of using GET variables in the URL won't work. Other than that, what do you think the best way would be to pull the songID from the external system and display the node in drupal with that songID?
Drupal menu system
It sounds to me like you're going to need to write a small custom module that uses the Drupal menu system. Technically, the entire path is passed to Drupal as a GET variable. (That's why yoursite.com/node/123 and yoursite.com/?q=node/123 do the same thing, assuming you have clean URLs enabled in your Drupal site settings.)
Basically, you'll want your module to watch for paths like thriller_by_michael-jackson/song/29581. (You can use % as a wildcard in place of thriller_by_michael-jackson). That path should point to a function that finds the song ID in the database and returns the appropriate node (probably via node_show). This might look something like:
You might want to add some error handling, etc., and you may need to make the SQL more sophisticated in case the song ID is different in different revisions of a given song node, but that's the basic idea.
I can't think of a good way to avoid going to the database for each song ID, but if you create a two-column index on field_song_id_value and nid in the content_type_song table, this shouldn't cause too many problems—especially if your server is running MEMCACHE or something like that.
nevets, what do you think?
Views?
You could probably use the Views module and Pathauto to do this. You can pass the songID as an argument through the URL, and use Pathauto to alias the URL.
But I have to ask: Why are you creating a second unique identifier for each song node (in addition to nid)? Even if you're trying to coordinate with some other database, for instance, you could do all of your Drupal work with the nid and store the songID as a CCK field. You could then access that field (e.g., in your template files) to interact with the other database.
Yes grobemo, I thought of
Yes grobemo, I thought of that, but I'm concerned about server load. I'm trying to keep from sacrificing performance as much as I can. The songID will be refreshed on a regular basis... so theoretically, the more concurrent visitors refreshing the node (by views) the more performance will take a hit, unless I'm missing something.
I hope my previous comments explained the 'why' a little better to give a clear context of this issue.