2

I am trying to write a views_query_alter hook and I want to identify the query I'm interested in. I have found the methods hasTag(), hasAllTags(), and hasAnyTag(), but I don't already know which tags I'm looking for. How can I get Drupal to tell me what tags already exist on queries? I didn't see any getTags() method.

3

I believe you mean hook_query_alter() here by mentioning hasTag(), hasAllTags(), and hasAnyTag() not hook_views_query_alter().

You can understand more about these methods from this documentation.

By help of these methods you can specify the targeted query, when you apply this in your module. A example code has been given in answer at Stackoverflow question, which helped me long ago to modify a query of views.

function MY_MODULE_query_alter(QueryAlterableInterface $query) {
  if ($query->hasAllTags('views', '[MY VIEW NAME]')) {
    $query->addJoin('INNER', 'taxonomy_index', 'ti', 'node.nid = ti.nid');
    $query->condition('ti.tid', '2,3', 'IN');
  }
}

If you are looking for the tags which you can use, just try to print $query inside the hook & see the result on the page where you are working. You will get what query you can target & the tags which you can use.

I hope it helps :)

  • So there's no way to get the query object to say what tags it has on it? I have to know what tag I'm looking for in the first place? – user1359 Jan 29 '14 at 17:52
  • What do you mean by tags here ? – RajeevK Jan 29 '14 at 17:56
  • What I mean is the string you are searching for when you call hasTag() on it. Instead of asking the query object if it has tag 'X', I want to call something like getTags() and have it return to me an array of 'X', 'Y', and 'Z'. I want it to tell me what tags it has instead of me asking it. – user1359 Jan 29 '14 at 17:58
  • 1
    Just try to DPM $query variable & see the result on the page where you are working. You will get what query you can target & the tags which you can use.. – RajeevK Jan 29 '14 at 18:00
  • @user1359 No need of separate answer, I just edited the same answer :) – RajeevK Jan 30 '14 at 4:45
0

Actually, hook_views_query_alter takes the view object and its query as arguments, so to identify the query I want to modify, I actually will look at the view object to see if it's the one I want.

  • 1
    hook_views_query_alter and hook_query_alter are different. Notably you can't nest condition groups in hook_views_query_alter – ummdorian Feb 26 '18 at 22:09
0

The right way to get all tags from a query is:

dpm($query->alterTags);

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.