1

I have this problem in getting a value from URL.

First I've created an url alias for a certain articles.

In example, from this /node/899 the alias I've created it's /news/investments/899

Then when I use arg(0) which is to get the value of first parameter which is "news" I get a result "node". The url alias works.

3 Answers 3

4

You can get aliased path using drupal_get_path_alias API Function..

$alias_path = drupal_get_path_alias();
$alias_arguments =  explode('/', $alias_path);
print $alias_arguments[0];
0

As you are using url alias ,you wont be able to get it using arg(0),Because your actual url is http://sitename.com/node/899

You can access the 'news' using request_uri function like as follow

$url_parts = explode('/',request_uri());

You can get 'news' in $url_parts[1]

Hope this will work for you

2
  • This doesn't work... For example if you access the node directly node/31 you will get "node" even though alias exists for the same and you are expecting "news"... And it breaks between local and live urls if you have basepath something like localhost/d7/node/31 and yoursite.com/node/31.. Not suggested to use request_uri method.. Instead use Drupal API Functions..
    – Anil Sagar
    Mar 24, 2014 at 6:48
  • @AnilSagar Yes you are right,But my solution will work if you are making url_alias of all url of that particular content type Mar 24, 2014 at 6:53
0

with this code you'll get the alias of the node/1. Then you can take the part you need with explode function.

drupal_get_path_alias('node/1');

if you want to get the alias of the current path, just write

drupal_get_path_alias();

for more info: https://api.drupal.org/api/drupal/includes!path.inc/function/drupal_get_path_alias/7

2
  • I have answered the same before you .. How your answer is different from mine ?
    – Anil Sagar
    Mar 24, 2014 at 6:45
  • i was testing my answer before i clicked to post it and i haven't seen yours, i pressed publish and get the -1. it was so fast.. thank for the present, this doesn't encourage people to work!!
    – angoru
    Mar 24, 2014 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.