How do I mark a database query with a tag so that I may alter it via hook_query_alter? I get the concept, but does anyone know what the syntax is? A working example would be helpful. Thanks.


In case you're struggling to find the documentation, the class is called SelectQuery, and you're looking for the addTag function.

$query = db_select('table', 't')->fields('t')->addTag('my_tag');

Then in your query alter:

function MYMODULE_query_alter(QueryAlterableInterface $query) {
  if ($query->hasTag('my_tag')) {
    // Do something...
| improve this answer | |
  • Note, if the query has a tag, you can also use hook_query_TAG_alter e.g: hook_query_my_tag_alter(QueryAlterableInterface $query){} similar to hook_form_FORM_ID_alter for forms. – David Thomas Jun 24 '13 at 9:06

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.