Can anyone explain this to me?

http://eve.cwrl.utexas.edu/mismatched_q.png

In this screenshot, "news" is an url alias for "node". The code in the body below is the output of:

var_dump($_GET['q']);

It looks to me as though Drupal is changing the query string. It gets a q of "news", checks the alias table to discover that "news" is another name for "node", and then re-sets the value of q to "node".

Why does it do that? It's annoying. Gets in the way every time I need to do something based on an url alias rather than Drupal's internal path. I can get the same info by using $_SERVER['argv'][0], but it requires extra processing.

Comments

coreyp_1’s picture

It happens here: http://api.drupal.org/api/function/drupal_init_path

It is done so that other modules can operate normally regardless of a path alias.

- Corey

wdmartin’s picture

Thanks for the answer, I appreciate it.

ckng’s picture

That's how drupal works, by internal path. You should use drupal_get_path_alias() to get your alias to work with.

CK Ng | myFineJob.com
consultation • web design & development • content development • site domain, hosting & maintenance • software design & development

wdmartin’s picture

The drupal_get_path_alias() function isn't always useful. It has two limitations.

First, in instances where one path has multiple aliases, drupal_get_path_alias() can only return one alias, which may not be the one I'm interested in. This has not yet been a problem for me, mostly because the UI for assigning additional aliases to a path is buried in the administration pages and hence rarely gets used.

Second, drupal_get_path_alias() can tell me an alias registered in Drupal's database; it cannot tell me what alias the user asked for.

It's that second problem I ran into yesterday. My boss wanted two things:

  1. A "from the director" message block on the front page next to the two most recent 'promoted to front page' items;
  2. A "news" page that would contain the same content as the front page, minus the "from the director" message.

The solution I found was to:

  1. Create a page-front.tpl.php with a custom region defined called $welcome;
  2. Assign a block containing the director's message to the welcome region;
  3. Create an alias specifying that "news" => "node";
  4. Jigger the page-front.tpl.php so the $welcome block isn't shown when accessed through the alias "news".

That last step was the big problem, because I needed to know what URL the user was staring at, NOT what alias might potentially exist for the page. Eventually I worked out that I could get that information through the $_SERVER['argv'] variable and set a template variable called $show_welcome using a preprocess function. Like this:

// Function from template.php
function phptemplate_preprocess_page(&$vars){

        // Get the arguments that were passed to the server, e.g. 'q=news' and split 'em into an array.
        $parts = explode('=', $_SERVER['argv'][0]);

        // Pop the 'q' off the front of the array.
        array_shift($parts);

        // Figure out whether we're looking at the 'news' page or not.
        if($parts[0] == 'news'){ $sw = false; } else { $sw = true; }

        // And finally set a variable for page-front.tpl.php that will determine whether to show the block or not.
        $vars['show_welcome'] = $sw;
}

It works nicely now. The site administrators can post pages, promote them to the front page, and they'll simultaneously appear on /news. I didn't have to reimplement anything. It was just annoyingly difficult to get access to the original path shown to the user rather than Drupal's internal path in order to show or hide the $welcome block.

ckng’s picture

Coupled with drupal_get_path_alias(), you just need to use arg() to check for what user path.

For your (4), just need to use php code to check if you should display the block (in your welcome block). There is no need to mess with preprocess or your page-front.tpl.php

<?php
  if (arg(0) == 'news') {
    return FALSE;
  }
  return TRUE;
?>

CK Ng | myFineJob.com
consultation • web design & development • content development • site domain, hosting & maintenance • software design & development