Drupal URL / URI alias variables into a Drupal Block

I found that it was not quite as easy as I had expected to pull uri alias variables into a Drupal block today. What I was looking for was the $path and for that matter, the $node variable but they are not available to the block. I did some researching but was unable to find any solutions to what was required for my block logic. So, I had to create my own query in order to find out what the uri alias was for a specific page the block was displaying on.

Here is my code, if anyone has a better solution please let me know.

<?php
// pull the non-aliased uri arguments
$src = arg(0) ."/". arg(1);
// select the url alias from the url_alias table
$sql = "SELECT dst FROM {url_alias} WHERE src = '%s'";
$path = db_result(db_query(db_rewrite_sql($sql), $src));

// I don't need the part of the string before the first /
$patharray = explode("/", $path);

// build the first part of the variable form the second item in the $patharray
$newpath = $patharray[1];

if ($patharray[2]) {
  //concatenate the second variable if available
  $newpath .= "/". $patharray[2];
}

?>

<a href="/constant/<?php print $newpath; ?>">New Path built from uri / url alias.</a>

The problem is the poor

The problem is the poor performance of the code. I use a little bit optimized solution.

Thanks for sharing your query

Thanks for sharing your query on finding out the uri alias for drupal. I have been searching for something like this for the past 2 hours lol. I admit my php is not that great so your query saved me time and my sanity.

I think it is not a best way

I think it is not a best way to query database, use uri() is enough. Thank you for posting.

Tutorial

Maybe this tutorial will give you some hints: http://www.tubesfan.com/watch/how-to-use-drupal-blocks . I've seen lots of them. Try to make it out for yourself.

drupal_get_path_alias()

The difference between url() and drupal_get_path_alias() is:
url() returns the uri of the node while the later returns the url alias

This only make sense if your have multi sites and each site has their own directory under one domain.

excellent post

excellent post

url()

I believe the url() function will do a conversion for you.

So you could take $_GET['q'], which is the current raw uri, and convert that with url() and get the alias.

Also, if you're creating a link, us the l() function which will do the url for you.

<?php
print l('New Path', $_GET[q]);
?>

This will return the HTML for your entire link, converting your path (the second argument) to it's alias.

See:
http://api.drupal.org/api/function/l/5
http://api.drupal.org/api/function/url/5

(D6) arg() and $_GET['q']

Not sure about Drupal 5, but in Drupal 6 arg() and $_GET['q'] don't even have the real, raw, uri data. I have a path /cart with an alias /join, and I want a block to only display on one of them. Both arg(0) and $_GET data both have "cart". Drupal must be modifiying $_GET to transform aliases to the real path.

A solution is to use request_uri(), which uses $_SERVER['REQUEST_URI'] (or equivalent), and DOES contain the raw uri.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
By submitting this form, you accept the Mollom privacy policy.