0

I've searched all over for examples of this, but I haven't found any. I'm trying to create an alias for path which includes query arguments, like profile?arg1=113.

It doesn't matter if I provide path_save() with the plain string representation of the path, or if I provide it with url().

url('profile', array('query' => array('arg1' => $uid)))

Either way, ? and = show up as escaped characters on the URL aliases admin page, which naturally means the path can't be found.

How can I keep the ? and = from being escaped?

12/19/12 Edit 1: the larger context is that I'm trying to set up the alias when a Profile2 profile is being saved (i.e., in mymodule_profile2_presave()) - that's when I'll have all the information I need to programmatically set up the alias.

12/19/12 Edit 2: I just realized that the problem isn't on the insert side - the url_alias table actually has unescaped characters in it. The problem is that Drupal doesn't urldecode the path before using it...

12/20/12 Edit 3: Found a solution using Redirect instead of path aliases. Redirect properly decodes the query string!

apaderno
  • 28,547
  • 16
  • 75
  • 90
LEN
  • 35
  • 1
  • 10

1 Answers1

0

You cannot attach the query string to the destination of an alias. The code executed from drupal_path_initialize() doesn't handle the query string correctly.

The function contains the following code.

$_GET['q'] = drupal_get_normal_path($_GET['q']);

Suppose that you have "example" as path alias that points to "node/93?uid=1"; that code would set $_GET['q'] to 'node/93?uid=1', while you are expecting $_GET['q'] to get 'node/93', and $_GET['uid'] to be set to 1.

What you could do is implementing hook_inbound_alter() with code similar to the following one.

function mymodule_url_inbound_alter(&$path, $original_path, $path_language) {
  list ($path, $query) = explode('?', $path);
  $_GET += drupal_get_query_array($query);
}
apaderno
  • 28,547
  • 16
  • 75
  • 90
  • Thanks for the quick answer, @kiamlaluno, but unfortunately, hook_inbound_alter() is not invoked with the alias I'm creating when I call path_save() - at least, in the context in which I'm calling it. The larger context is that I'm trying to set up the alias when a [Profile2](http://drupal.org/project/profile2) profile is being saved (i.e., in mymodule_profile2_presave()) - that's when I'll have all the information I need to programmatically set up the alias. I added mymodule_url_inbound_alter(), but it is only invoked with the path for editing the user's profile when I save that profile. – LEN Dec 19 '12 at 16:37
  • I am sorry; I was not clear in that part. `hook_inbound_alter()` is used to alter a URL before it is used from Drupal. The code I am using is used to change a path like node/93?uid=1 to node/93, and to set `$_GET['uid'] to 1. It is not used to alter what `path_save()` saves, but it avoids a path like node/93?uid=1 is used to look for a node with ID equal to 93?uid=1. – apaderno Dec 19 '12 at 18:16
  • no problem. I should have provided the larger context when I made my original post (I'll add that info to the post right now). – LEN Dec 19 '12 at 18:38