2

I am new to Drupal 7 and am trying to do the following:

1) I have a node that's aliased to domain.com/inventory - the node has code that'll generate dynamic content.

2) My site creates dynamic URLs like domain.com/inventory/ABCD and I need to know how to a) figure out that I'm on the /inventory page and b) extract the argument "ABCD" from the URL.

Can you please help me?

3 Answers 3

2

If your node is 12, your internal path is node/12 when the alias is 'inventory'. In that case arg(0) gives you 'node' and arg(1) gives you '12'.

In the case of dynamic URLs like inventory/ABCD, I assume that the internal path is 'inventory/ABCD' (so that inventory/ABCD does not have anything to do with node 12). Then arg(0) gives you 'inventory' and arg(1) gives you 'ABCD'.

3
  • arg(0) and arg(1) actually gives me 'node' and '12'. I was able to get the path using drupal_get_path_alias();
    – Roger
    Sep 11, 2011 at 18:23
  • I couldn't give a concret answer without knowing the mechanism of your menu system and what is your "real" path. Try to look at $_GET['q'] or arg(2) (if available). I think your site uses hook_menu to add aliases like inventory/ABCD to node/12/ABCD, so your real (internal) path is node/12/ABCD and arg(2) = 'ABCD'.
    – jcisio
    Sep 12, 2011 at 9:20
  • I was able to get node/12/ABCD and arg(2) = 'ABCD' to work. Thanks.
    – Roger
    Sep 12, 2011 at 17:45
2

I think what you need is drupal_get_path_alias.

$current_path = drupal_get_path_alias();

You can then explode that path on '/' to get your argument.

3
  • My problem is that I can't get a node (/inventory) to also display (/inventory/ABCD), so I can't get to a point to extract the variables.
    – Roger
    Sep 11, 2011 at 18:25
  • Well in that case you can try using that path alias > patterns and see if there are any tokens that could be use for populating that variable. If not i would make a hook menu in a custom module but their might be a better way..
    – silkAdmin
    Sep 11, 2011 at 22:13
  • Got node/12/ABCD and arg(2) = 'ABCD' to work for now. I think I'll just live without the alias.
    – Roger
    Sep 12, 2011 at 17:45
0

The answers here are pretty good, also look at drupal_lookup_path() and request_uri() for related functions around what you're trying to do.

4
  • If my node is /inventory, how do I even get Drupal to display /inventory/ABCD, which is technically not a real node, but a node /inventory, with an argument 'ABCD'
    – Roger
    Sep 11, 2011 at 18:26
  • actually, your node is always using the system path "node/XXX" where XXX is the node ID, and if you're displaying your node at "inventory/YYY" then you're using an alias "inventory/YYY", not an alias "inventory" with an argument "YYY". arg() will always return the system path, but request_uri() will return the current url (excluding the domain) as a string even if it is an alias. It shouldn't be too hard to parse that string to get out the "argument" if that's really the way you want to do things here. Sep 12, 2011 at 2:03
  • Thanks. I got node/12/ABCD and arg(2) = 'ABCD' to work for now.
    – Roger
    Sep 12, 2011 at 17:45
  • 1
    why not just do something like $url = request_uri(); $url = ltrim( $url, '/' ); $url_array = explode('/', $url);? Sep 21, 2011 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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