I have a view whose query I want to completely change. I want to change the select statement, the joins and the where clause. the original query is this:
SELECT node.nid AS nid, node.title AS node_title, field_data_field_location.field_location_tid AS field_data_field_location_field_location_tid, node.created AS node_created, 'node' AS field_data_field_location_node_entity_type, 'node' AS field_data_field_longtext_node_entity_type
FROM
{node} node
LEFT JOIN {field_data_field_entity_type} field_data_field_entity_type ON node.nid = field_data_field_entity_type.entity_id AND (field_data_field_entity_type.entity_type = :views_join_condition_0 AND field_data_field_entity_type.deleted = :views_join_condition_1)
LEFT JOIN {field_data_field_location} field_data_field_location ON node.nid = field_data_field_location.entity_id AND (field_data_field_location.entity_type = :views_join_condition_2 AND field_data_field_location.deleted = :views_join_condition_3)
LEFT JOIN {node} node_1 ON node.nid = node_1.nid AND (node_1.language = 'bg' AND node.tnid != node.nid AND node.language = 'en')
WHERE (( (field_data_field_entity_type.field_entity_type_tid = :field_data_field_entity_type_field_entity_type_tid ) AND (field_data_field_location.field_location_tid = :field_data_field_location_field_location_tid ) )AND(( (node.status = :db_condition_placeholder_4) AND (node.type IN (:db_condition_placeholder_5)) AND (node.language IN (:db_condition_placeholder_6, :db_condition_placeholder_7)) )))
ORDER BY field_data_field_location_field_location_tid ASC, node_title ASC, node_created DESC
and the new query is this:
select CASE when b.node_title is not null then b.nid else a.nid end as nid,
CASE when b.node_title is null then a.node_title else b.node_title end as node_title,
CASE when b.node_title is not null then b.field_data_field_location_field_location_tid else a.field_data_field_location_field_location_tid end as field_data_field_location_field_location_tid,
CASE when b.node_title is not null then b.node_created else a.node_created end as node_created,
'node' AS field_data_field_location_node_entity_type, 'node' AS field_data_field_longtext_node_entity_type
from
(SELECT node.tnid, node.nid AS nid, node.title AS node_title, field_data_field_location.field_location_tid AS field_data_field_location_field_location_tid, node.created AS node_created, 'node' AS field_data_field_location_node_entity_type, 'node' AS field_data_field_longtext_node_entity_type
FROM
node node
LEFT JOIN field_data_field_entity_type field_data_field_entity_type ON node.nid = field_data_field_entity_type.entity_id AND (field_data_field_entity_type.entity_type = 'node' AND field_data_field_entity_type.deleted = '0')
LEFT JOIN field_data_field_location field_data_field_location ON node.nid = field_data_field_location.entity_id AND (field_data_field_location.entity_type = 'node' AND field_data_field_location.deleted = '0')
WHERE field_data_field_entity_type.field_entity_type_tid = '818'
AND field_data_field_location.field_location_tid = '408'
AND node.status = '1'
AND node.type IN ('data_entity')
AND node.language = 'en') a LEFT OUTER JOIN
(SELECT node.tnid, node.nid AS nid, node.title AS node_title, field_data_field_location.field_location_tid AS field_data_field_location_field_location_tid, node.created AS node_created, 'node' AS field_data_field_location_node_entity_type, 'node' AS field_data_field_longtext_node_entity_type
FROM
node node
LEFT JOIN field_data_field_entity_type field_data_field_entity_type ON node.nid = field_data_field_entity_type.entity_id AND (field_data_field_entity_type.entity_type = 'node' AND field_data_field_entity_type.deleted = '0')
LEFT JOIN field_data_field_location field_data_field_location ON node.nid = field_data_field_location.entity_id AND (field_data_field_location.entity_type = 'node' AND field_data_field_location.deleted = '0')
WHERE field_data_field_entity_type.field_entity_type_tid = '818'
AND field_data_field_location.field_location_tid = '408'
AND node.status = '1'
AND node.type IN ('data_entity')
AND node.language = 'bg') b
ON a.nid= b.tnid
I tried with views_pre_execute hook and with query_alter view with no luck... The first attemt is using https://drupal.org/node/409808 $view->build_info['query'] = MY QUERY; which throws error:Call to a member function addMetaData() on a non-object
The attempt with query alter is a dissaster...I cannot make it work..