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>


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.

excellent post

excellent post

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.

Behind this it's a big

Behind this it's a big history and you can get what is running well, for this kind of history if you need any type of notes then we will guide you. Assignment Writing | coursework | Dissertation

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.