I'm getting no love from hook_query_alter. I have a simple query to which I want to add a tag, while also adding a node_access tag. Thus the whole query looks like this:

$the_nodes = db_select('node', 'n')
     ->condition('n.type', 'some_node_type')
     ->fields('n', array('nid', 'title'))

I then define a handler in one of my modules and litter it with debugging statements, hoping something will happen:

function mymodule_query_alter(QueryAlterableInterface $the_query) {
    dsm("inside mymodule_query_alter");
    if ($the_query->hasTag('my_custom_tag')) {
        dsm('Found it!');
        // do real work here

I then clear the system cache. Once I've done this, the following things happen:

  • My handler is running, and reports a number of invocations from various system calls.
  • The objects being dumped by the dsm calls are all very much alike -- they contain only an alterTags array, which typically has entries for either node_load_multiple or file_load_multiple, but nothing else -- certainly nothing like a full Query object.
  • None of the alterTags arrays have an entry for my_custom_tag.
  • The inner "Found it!" message never fires.

Arggh. This is my first encounter with hook_query_alter(), and I'm probably just missing something basic about its use. Can anybody clue me in? (BTW, based on some browsing around, I've also tried defining handlers for my_module_query_my_custom_tag_alter and my_module_query_node_access_alter, but never see anything from them.)

  • Your implementation of hook_query_alter seem correct for me. Are you sure you query is executed?
    – gagarine
    Mar 19, 2013 at 23:22
  • Well, there's progress. I now have my_module_query_alter() finding my_custom_tag. If I do dsm($the_query), all I see is the alterTags array. But I'm apparently getting the query object, because I can dump (string)$theQuery and see the sql version of the query, and I can do $the_query->orderBy('n.title'), and the ordering will happen. So, yay, I guess. But shouldn't there be some way for me to see the query object itself, so I can figure out how to make the changes in the query that are the whole point of being here?
    – Jim Miller
    Mar 20, 2013 at 0:01

1 Answer 1


Just to close this item out: It appears that my definition of hook_query_alter was, in fact, working correctly. My problem was in assuming that I would be able to do things like dsm($the_query) and see the components of the query object in preparation for doing stuff with them. This doesn't seem to be the case -- dsm() wasn't returning anything of real use other than alterTags, but the orderby() (and other similar actions) did in fact alter the query. So, I'd argue that it's a bit confusing, but works-as-designed, I guess...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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