My goal is to retrieve an url from a link field that belongs to the node(nid) inside an url. With that info I will be able to redirect to that url through this script.
The situation
From a product page I have an url "sitename.com/visit/?id=nid". This is configured inside the node--[content-type].tpl.php
The root folder "visit" contains a php redirect script named "index.php"
The script contains the following code which should get the nid from the url and use it to collect a node url inside a link field.
$id = $_GET["id"];
if (isset($id))
{
$link = mysql_connect('localhost', 'db_user', 'passwoord')
or die("MySQL is broke: " . mysql_error());
mysql_select_db('db_name');
$query = db_query("SELECT node.nid AS nid, 'node' AS field_data_field_deeplink_node_entity_type
FROM
{node} node
WHERE (( (node.nid = '$id' ) ))");
$result = mysql_query($query) or die("Error while processing query");
$line = mysql_fetch_array($result, MYSQL_ASSOC);
$url = $line['node'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$url);
exit;
}
else
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.sitename.com');
}
I used views to get the query but I'm stuck. I get confused with the changes of the update to D7 and the introduction with entities. And since the folder and script is in fact outside of the drupal structure I'm not sure if I'm approaching this the right way.
Cheers,
David
Comments
From a product page I have an
where is this coming from?
if you have the url when you show the page why not show it as the final link?
Sorry, yes that would be the
Sorry, yes that would be the most logical way to approach this if it where not for the fact that the urls are outboud and from a datafeed. The urls are very long and they have no meaning to visitors, so I need to serve them a shorter link that is more understandable.
Please Elaborate
Hi,
Are you writing a new Drupal module? Or are you trying to modify the behavior an existing Drupal module?
In either case, you should be writing your own custom module. Don't modify existing code. Implement hooks to alter behavior.
The module developer's guide is located here: http://drupal.org/developing/modules
The module examples are located here: http://drupal.org/project/examples
The source code of your current Drupal installation is also a great resource.
This forum is for folks who are writing Drupal module code. See the forum description here http://drupal.org/node/644164 . You might want to read.
Once you've begun your Drupal module and have a specific API question, then I would post here :)
Good luck :)
At the moment I'm not doing
At the moment I'm not doing either of those. The only thing that is modified is a node.tpl to pass the nid to the script. The script above resides outside of any Drupal module or core so I'm not modifying anything. At this stage I'm looking for support to make the script (retrieving the url from db) work.
It could become a module in time but for now I will post my question elsewhere. Thanks for your replies and sorry for the mistake (inside the description, "Code related questions" made me feel I was in the right place) of posting this here. If it will become a module I will use this thread for possible questions or input.
Cheers!