Closed (fixed)
Project:
Views (for Drupal 7)
Version:
6.x-2.x-dev
Component:
Miscellaneous
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
6 Oct 2010 at 14:32 UTC
Updated:
25 Oct 2010 at 08:50 UTC
Hello,
Suppose I'd like to add an item that looks like that
CASE MailAddress
WHEN 'AdPro1' then AddressPro1Line1
WHEN 'AdPro2' then AddressPro2Line1
WHEN 'AdPer1' then AddressPer1Line1
WHEN 'AdPer2' then AddressPer2Line2
ELSE 'Unknown Address'
END AS DeliverAddressLine1
Is it possible to do that programmalically using hook_views_data ?
I know how to do it when it's an existing field in a table.
What's the way for my purpose ?
(I'd like to avoid using hook_views_query_alter because i want the people to freely
use this field like item on the client side)
Thanks for your help !
Kenjil
Comments
Comment #1
dawehneryou have to define your own handler.
Therefore
a)
set the 'handler' key to a custom name
b)
implements hook_views_handlers and define the handler there
c) add the yourhandler.inc file
d) extend the views_handler_field php class and override the query method. Therefore copy
the existing query method in views_handler_field and change the code.
Comment #2
kenjil commentedExcellent,
I understand better views now.
Thank you.
Now I have another question, suppose I want to add some condition so that i can filter using a HAVING CLAUSE, for example,
The reason I'd like to do that is one can not use an ALIAS in a WHERE clause in SQL.
It seems that filter are always pushed to WHERE clause (Am i wrong here ?).
Question :
Is it possible to implement this HAVING filter by extending
views_handler_filter_string (and overiding the some methods) or should I look for a workaround ?
Thanks again for your help,
Kenjil
Comment #3
dawehnerThere is $view->query->add_having but i'm not sure whether it really works.
Comment #4
kenjil commentedStill not fixed for the first problem,
I extended the class query of views_handler_field by overriding query method :
This doesn't works. It happens that when views api is constructing the query, it preffixes with 'Table.' the complex select expression ending up in a SQL error (here Table.CASE doesn't make sense):
So i'm asking myself, did i choose the wrong way or is views2 missing an add_complex_select_expression which would add a 'complex select expression' without adding the 'Table.' as prefix as it is now defined in method views_query::query ?
Thanks again, Kenjil
Comment #5
dawehnerYou can access the table name which views uses with $this->table_alias
Comment #6
kenjil commentedFixed. If table field is '' when calling add_field, no table name is prefixed.
For the filter side, I gave up the idea of using a having clause which creates SQL problem where mixed with ORDER BY clause. I use add_where with reformulating my complex select statement instead of using its alias.
Everything works like a charm.
Thanks, Kenji
For the person who wants to see the code, here is how to extend views_handler_filter_string :