1

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.

1 Answer 1

2

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...
  }
}
1
  • 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. Jun 24, 2013 at 9:06

Your Answer

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

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