5

Jumping off of this question, I'd like to know how to gracefully detect and remove each snippet in the WHERE clause of my query object in plain old hook_query_alter(), not hook_views_query_alter() (as the Views query object seems structured in some entirely different way (frustrating!). The documentation vaguely suggests that this is possible by showing how you can create a reference to your query conditions that you can then manipulate, by starting with this:

$where =& $query->conditions();

... but the object(s) within are utterly confusing, and I've been struggling to remove or replace existing snippets. I've explored /includes/select.inc and /includes/query.inc but the classes therein don't seem to provide any methods for fine-tune manipulation.

8

That's pretty much all there is to it: in hook_query_alter() you only have access the query object from "outside" and, since there is no public API to remove conditions, it's kinda confusing.

However, there's some clever php hackery going on so if you call $where =& $query->conditions(); or any of the other examples in the documentation you linked it will return a reference to that corresponding variable.

So, in the where example above, you gain access to $query->where protected class variable. If you inspect them you will see they are just basic php arrays containing all of the information and you can literally just unset something on that reference and be done.

So:

$where =& $query->conditions(); // Remove the second condition. unset($where[1]);

Finally, specifically with where queries, you want to be mindful of the #conjunction and eventual more complex and/or grouping stuff.

  • 1
    Well played sir, well played. – Patrick Mar 8 '16 at 15:44

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.