I am developing a module that exposes a public API, which includes functions to save data in his own database table, which is supposed to be used from any module that uses my module.

The module checks the content of the database to verify if a user is not allowed to access the site, or if users should be notified with an error message when they change something in their user profiles.
Considering that in most of the cases, the database table is the only table to check, should I change the code to allow third-party modules to alter the query being executed from my module through hook_query_TAG_alter() (or the more generic hook_query_alter()), or use a custom hook that other modules need to implement?


Not really sure what your question exactly is now.

  • I'm not sure why hook_query_alter() even exists, it did not in the beginning. I don't think that hook makes much sense, I can't think of a use case where you want to alter every query.

  • If you expect that other modules want to extend your query, then use "->addTag('yourmodule_tag')" so that it is possible to alter it. As you already said, this only covers cases where the same existing query should be changed. It might be possible to do other checks and then add a condition that can never be true (like 1 = 0) on the query, but that sounds hacky to me.

  • You can also define your own hook if you think that there could be more complex checks that other modules want to do. You probably want to re-use the hook_node_access pattern with three possible return values (ignore, allow, deny), see node_access().

You can do either or both of these two things, hook calls are rather fast in D7 because the hook implementations are cached and don't need to re-checked every time the hook is invoked.

  • I think hook_query_alter() exists to allow a module to alter more than one query (especially when the code executed to alter the queries is the same for every query), not to allow a module to alter every SQL query. That is similarly to what happens with hook_form_alter(), which is not supposed to be used to alter all the existing forms, but to allow a module (for example) to add the same field to more than one form. – kiamlaluno Mar 7 '11 at 15:57
  • 1
    That is true, but in contrast to hook_form_alter(), there is no equivalent to $form_id so there is no standard way to uniquely identify a specific query. And most reasons for using hook_form_alter() instead of hook_form_FORM_ID_alter() have been eliminated in D7 (for example, the order is now fully depending on the weight while in D6, form_id alters came always first, base_type and so on). Also, according to api.drupal.org/api/drupal/includes--database--select.inc/…, hook_query_alter() is only called if a query has at least a single tag. – Berdir Mar 7 '11 at 16:14
  • 1
    What I was trying to say above: I think is better/faster to define two separate query_TAG_alter hooks than using query_alter() because the latter could be called quite often on a single page. I haven't actually tested it though :) – Berdir Mar 7 '11 at 16:26
  • There isn't $form_id, but there is $query->hasTag(), where $query is the parameter passed to hook_query_alter(). I understand what you are saying; I am just saying that I am not sure I would always use hook_query_TAG_alter() in the same way I would not always use hook_form_FORM_ID_alter(). – kiamlaluno Mar 10 '11 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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