When we bootstrap Drupal, we do this:
// Initialize $_GET['q'] prior to loading modules and invoking hook_init().
if (!empty($_GET['q'])) {
$_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
}
else {
$_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
}
Why do we put the results back into $_GET['q'] and not into a Drupal specific global variable? Or in a function that stores it statically? It seems to me that the above example loses on several counts:
1) we destroy the original $_GET['q'] (don't know whether this is useful in some cases)
2) we encourage people to access the path over and over again using $_GET['q'] which is lots of typing and not very descriptive, and locks us in to this one parameter.
3) makes it unintuitive to alter or change the value (you end up having to reassign $_GET['q']).
I'd suggest that instead we do something like this:
global $drupal_path;
// Initialize $_GET['q'] prior to loading modules and invoking hook_init().
if (!empty($_GET['q'])) {
$drupal_path = drupal_get_normal_path(trim($_GET['q'], '/'));
}
else {
$drupal_path = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
}
Now, for the real-life situation that prompts me to ask the question. I've written an application that updates a table (made with theme_table) with AJAX in response to changes in taxonomy selectors. The AJAX PHP returns the entire table new, including the sorting and paging links. Since the path to the AJAX function is different than the path to the page that builds the original table, the sorting and paging gets broken after the first AJAX update because they both use $_GET['q'] in their code. So clearly, I'm going to have to reassign $_GET['q'] in the AJAX function to avoid this. It just seems really bad practice to me to do this. The stuff handed over from the server should be holy territory, never touched, immutable. It would seem much more intuitive to me to either reassign a global $drupal_path variable, or use a drupal_set_normal_path() function to do the same (and use drupal_get_normal_path instead of $_GET['q']).