Get current page/url in a hook

18,378

You want http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/request_path/7.

For Drupal 6, one has to use $_GET['q'].

Share:
18,378
stefgosselin
Author by

stefgosselin

** PHP / LAMP developper ** I am a 44 yr. old french Canadian PHP developper who has been freelancing and offering consulting services for the past 15 years. I have had the opportunity to cater to a diverse range of clients: manufacturers, pharmaceutical companies and media corporations among others. I am interested in anything related to Open-Source Web developpment. I am curious on many subjects and aspects of programming: frameworks, design methodologies, testing methodologies, security, analysis and requirement coding tools, and any computer or stat related math.

Updated on June 04, 2022

Comments

  • stefgosselin
    stefgosselin almost 2 years

    In hook_css_alter, I want to disable style-sheets on specific pages. Was ok for the front page requirement with following snippet:

    function morin_css_alter(&$css) {
      if(drupal_is_front_page()){
        foreach ($css as $data => $item) {
          unset($css[$data]);
        }
      }
    }
    

    Now I need to remove specific styles on a photo-album page. I have thought of a few ways to do this, but I'd like to do it the drupal way.

    The closest function I have found to do this is drupal_get_destination() , I don't think it's meant for this but it does return the current path in an array, as shown by the following snippet added in css_alter hook.

    echo " PATH = " . var_dump(drupal_get_destination());
    
    Output: PATH = array(1) { ["destination"]=> string(6) "photos" }
    

    Is this the recommended way of getting the path from inside a function/hook, or is there some variable in global namespace I should use instead?

  • Nadeem Ahmed
    Nadeem Ahmed about 13 years
    drupal_get_destination() doest not always return the current path. The current path is actually the default value when no other destination (to be used in a redirect in case of, for instance, a successful form submit) has been provided. See the drupal_get_destination()'s documentation.
  • Berdir
    Berdir about 13 years
    I said nothing about drupal_get_destination(), I assume you meant that as an additional information?
  • Nadeem Ahmed
    Nadeem Ahmed about 13 years
    Yep, this is additional information as the original question mentioned using drupal_get_destination(). Sorry, I should have made it clearer.
  • stefgosselin
    stefgosselin about 13 years
    Hey guys thanks for the insight, just was I was looking for. drupal_get_destination did not feel right for this, now I see why.