[development] hook_views_query_alter

Earl Miles merlin at logrus.com
Sat Oct 30 00:48:44 UTC 2010

On 10/29/2010 4:18 PM, Ashraf Amayreh wrote:
> Hello all,
> I've searched the net all over for the particular case I have but
> couldn't find an answer. I have a view with an argument, based on this
> argument, I would like to either pass it through, or join to two more
> tables and add a where clause to my query that is pretty unrelated to
> the argument type. For example, let's say the argument is a tid and I
> want to add a where clause to the files table if this tid was equal to
> 5, otherwise, I want it to function normally. For this I looked into the
> hook_views_query_alter hook
> The problems I faced in order:
> 1. The cck file field that contains the fid was not in the fields list,
> so the table wasn't even in the joins, I resolved this by adding the cck
> file field to the fields list although I have no use for it. But this at
> least added the table to the view object in this hook (I didn't really
> want to add a field in vain just to get its table to show up in the
> view->query object).
> 2. After doing that, I needed to do a join from the table in #1 to the
> files table, again, the files table was not inside the view->query and
> this is where I got stuck.
> 3. Assuming #2 works, I will finally add a where clause to the files table.
> 4. Let's say I did all this successfully, I would need to remove the tid
> that was passed in the argument so that it would not be rendered inside
> the query (i.e. where tid = 5).
> Is there any guidelines on how to fully handle the view, view->query
> objects? All I could find were hacks to add sort by, add simple wheres
> to tables that were already in the views object, and to create aliases
> for tables that are, again, already in the view object. None of them
> with the complexity I need.
> I've hesitated on weather this should be in the views issue queue or
> here, but I think many agree views is no longer an optional module. If I
> do get this resolved I'll be more than happy to post it on our site or
> to create a views issue with the details.

Can you accomplish what you want to do with two different views? Create
your own page handler, examine your argument, and pick which display to
use? Or write an access plugin, which is really very rare, which does
this, and let Views pick the display based upon the access status?

More information about the development mailing list