So I am attempting to parse out the URL, so that I can get a specific page template to be used for a specific URL; if the URL is example.com/team, then use page-team.tpl.php.

However, when I do this, the URL I am getting (via $_GET['q']) always seems to be the URL from where I was, not the one where I am currently at. If I directly navigate to example.com/team/surf, it gives me "team/surf"; if I go to example.com/team it gives
"team/surf." If I refresh the page, it will then give me the desired one.

I dropped this code into my template.php file.

function theme_preprocess(&$vars, $hook) {
  switch ($hook) {
    case 'page':   
      $normal_path = trim($_GET['q'], '/');
      $path_alias = drupal_get_path_alias($normal_path);
      $alias_parts = explode('/', $path_alias);

      if (($alias_parts[0] === 'team') && (count($alias_parts)) == 1) {
       $vars['template_file'] = 'page-team';
      }
      break;
    }

Do you have any idea on what is going on here?

link|improve this question
1  
Consider posting your solution as an answer and marking it as accepted so that others can easily find a solution – Laxman13 Feb 11 at 17:21
yup. didn't realize that. i'll have to update the answer in 4hrs. – austin Feb 11 at 17:59
It seems a localized situation; $_GET['q'] is normally updated with the current value get from the page request. I have never seen that value being not correct. There could be code that alters that value, but from what being reported is not possible to know that. – kiamlaluno Feb 11 at 21:09
feedback

migrated from stackoverflow.com Feb 11 at 18:15

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 2 down vote accepted

I'm not sure why your code happens to fail in some cases, and in some cases not. But what I would try is replacing that code with the provided by Drupal arg function.

On "team/surf", arg(0) will return "team", and arg(1) "surf". This is what I use myself, and hopefully that will solve the problem for you.

link|improve this answer
interestingly it came down to dsm(). So i wanted to check some variables before i moved forward. i was simply dumping them into dsm() just to see what my results were looking like. at the time, just seemed like a good idea. so dsm($_GET['q']) would output the previous url and not the current url. when i did a print $_GET['q'] it would show me the current url as expected. as for arg(). good call on that one. i have seriously forgotten all about that one. thanks for the help. – austin Feb 11 at 22:16
@austin That is what happens sometimes with dsm(). – kiamlaluno Feb 12 at 1:53
THEME_preprocess_page() is called after the messages HTML has been produced. So, when drupal_set_message() or anything that ends calling it is called from THEME_preprocess_page(), the message will only be displayed on the next page request. It explain why dsm($_GET['q']) prints the previous URL. Also, note the dpm() should be used instead of dsm(). – Pierre Buyle Feb 13 at 0:24
awesome. thanks for the response. – austin Feb 13 at 13:39
feedback

Your Answer

 
or
required, but never shown

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