jquerymoduledrupal-7clean-urlshook-menu

Drupal 7 menu but no clean-urls


I am in the process of developing a module which needs to make an ajax call to a menu path defined by the same module.

Below is the hook_menu function:

function staff_filter_menu(){
        return $items['staff/filtering/results'] = array(
            'page callback' => 'staff_filter_function',
            'type' => MENU_CALLBACK,
        );
    }

    function staff_filter_function(){
        drupal_json( array('status' => 0, 'data' => "staff_filter_function RESPONDING!"));
    }

And the js code:

(function($){

$(document).ready(
function(){
    $results = $.get('http://localhost/test1.localhost/?q=staff/filtering/results');

    //$results = $.get('http://localhost/test1.localhost/?q=admin/config/people');
    //$results = $.get('http://localhost/test1.localhost/admin/config/people');
});

})(jQuery);

The first line (the call to my defined menu) fails and returns a 404 error in firebug.

So, I tried an existing random menu which are the 2 following menus. But I made one work with clean URLs and the other without. The existing menu set with a clean URLs also failed but the same menu which isn't a clean URL worked fine.

Because my localhost installation is not happy to work with clean URLs regardless of what I try, I need a technique to make my module flexible regardless of if the site is working with or without clean URLs (particularly if I end up releasing it).


Solution

  • You can use the Drupal function URL. This will adjust accordingly if clean URL's are on or off.

    For example:

    <a href="<?=url('node/' . $node->nid, array('alias' => FALSE))?>">
    

    Will return either:

    ?q=NODE_ALIAS if clean URL's is off

    OR

    NODE_ALIAS if they are turned on.

    This has saved me loads of hassle as I have a similar issue with my localhost. For more info on the URL function - http://api.drupal.org/api/drupal/includes%21common.inc/function/url/7