4

I'd like to retrieve the current page path alias without the installation's folder arguments. I'm using:

drupal_get_path_alias(request_uri())

But this returns installation/whatever/actual/path and I want to retrieve the actual/path only no matter what installation/whatever is.

Thanks in advance :)

apaderno
  • 28,547
  • 16
  • 75
  • 90
ozke
  • 1,622
  • 4
  • 25
  • 44

7 Answers7

9

Found it. It was actually a mix of both suggestions:

$current_path = drupal_get_path_alias($_GET["q"]);

Thanks though.


Update: the previous solution doesn't always work

Someone suggested using an alternative method:

str_replace(base_path(), '', drupal_get_path_alias(request_uri(), 1));

But, is there any way of doing the same without using the slightly expensive str_replace?

Thanks

ozke
  • 1,622
  • 4
  • 25
  • 44
2

The path of the node you are on?

http://api.drupal.org/api/function/drupal_get_path_alias/6

if ($node || (arg(0) == 'node' && arg(2) != 'edit')) {
   $system_path = 'node/'.arg(1);
   $current_path = drupal_get_path_alias($system_path);
}

That code will fire on node pages and tell you the page alias.

For more information, you can dump out $_GET and look at the 'q' query string value.

Kevin
  • 13,153
  • 11
  • 60
  • 87
1

Maybe you can use base_path() and str_replace like this :

str_replace (base_path(), '', drupal_get_path_alias(request_uri()), 1);

The base_path is saved in the database.

Brice Favre
  • 1,511
  • 1
  • 15
  • 34
1
$current_page_url = drupal_get_path_alias( implode( '/', arg() ) );

also works

bstpierre
  • 30,042
  • 15
  • 70
  • 103
chim
  • 8,407
  • 3
  • 52
  • 60
0

Fastest solution.

substr(drupal_get_path_alias(request_uri(), 1), strlen(base_path()));

To pass it into an argument

$arg = explode('/', substr(drupal_get_path_alias(request_uri(), 1), strlen(base_path())));

Timofey Drozhzhin
  • 4,416
  • 3
  • 28
  • 31
0

Have you tried $_GET['q']?

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • That returns the path, not the alias. Thanks though, I already found it. – ozke Jun 08 '10 at 14:46
  • Enable the Global Redirect module and the path should always be that of the alias. – ceejayoz Jun 08 '10 at 15:24
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – netcoder Aug 23 '12 at 14:31
  • @netcoder You can consider the answer "try $_GET['q']", then. You really are bumping a two year old thread for this? – ceejayoz Aug 23 '12 at 14:49
  • @ceejayoz: No, it's the review tool that posts comments automatically. Clearing up the flagged answers, and this should probably have been a comment. – netcoder Aug 23 '12 at 14:50
0

I tend to plump for the full url instead to avoid any problems that arise when base_path() isn't just '/'.

$url = (($_SERVER['HTTPS'])?"https://":"http://")  .  $_SERVER['HTTP_HOST'] . url($_GET['q']);

or even more simply:

$url = url($_GET['q'], array('absolute'=>true));
Al.
  • 173
  • 1
  • 11