I have a very simple view that lists all the nodes, which shows the following query:
SELECT node.nid AS nid, node.title AS node_title, node.uid AS node_uid, node.created AS node_created, 'node' AS field_data_field_site_node_entity_type, 'node' AS field_data_field_status_node_entity_type
FROM
{node} node
WHERE (( (node.status = '1') AND (node.type IN ('ticket')) ))
ORDER BY node_created DESC
LIMIT 50 OFFSET 0
I am trying to overwrite it with a custom database query to list only nodes of the user referenced in the URL and all his invitees. This is the code I am using.
function my_custom_module_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == "support" && $display_id == "page" ) {
// Let's start collecting uids and put current page uid as the first member of array.
$uids[] = arg(1);
// Now let's collect all invitees of the page uid author and push into the same array.
$query = db_select('invite', 'i')
->condition('i.uid', arg(1), '=')
->condition('i.status', '3', '=')
->fields('i', array('invitee'));
$result = $query->execute();
foreach ($result as $record) {
$uids[] = $record->invitee;
}
// Now let's pull all the nodes of the inviter (page uid) and his invitees.
$query = db_select('node', 'n')
->condition('n.type', 'ticket', '=')
->condition('n.status', '1', '=')
->condition('n.uid', $uids, 'IN')
->fields('n', array('nid', 'title', 'created'));
$result = $query->execute();
foreach ($result as $record) {
dpm($record->nid.' | '.$record->title.' | '.$record->created);
}
}
}
dpm()
shows all the correct nodes, but reading hook_views_query_alter() I still can't figure out how to make my view use this query.
How can I achieve this?