Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to run a subquery through views and found this call. I cannot get it to work. Here is how I am using it

function module_name_custom_views_pre_execute(&$view) {
    if($view->name=="view_name") {
            if($view->build_info['query_args'][1]==0) {
            $view->build_info['query']="my subquery";

                //echo ($view->build_info['query'] );

Without to test it I tried replacing the $view->build_info['query'] with the query generated in the view. Instead of a view result I get a blank page. Any other query I put in there myself results in a blank page as well.

I am writing the queries like so:

$view->build_info['query']="SELECT nid FROM node WHERE node.status='1'";

So I am not assigning any array objects values but rather writing the query in as if I would in any other db call. Should I build the query through the means of creating an object? So something like this to start:

["build_info"]=> array(3) { 
    ["query"]=> object(SelectQuery)#752 (21) { 
        ["fields":protected]=> array(4) { 
            ["nid"]=> array(3) { 
                ["field"]=> string(3) "nid" 
                ["table"]=> string(4) "node" 
                ["alias"]=> string(3) "nid" } 
        ["node_created"]=> array(3) { 
            ["field"]=> string(7) "created" 
            ["table"]=> string(4) "node" 
            ["alias"]=> string(12) "node_created" } 
        ["og_og_membership_label"]=> array(3) { 
            ["field"]=> string(5) "label" 
            ["table"]=> string(16) "og_og_membership" 
            ["alias"]=> string(22) "og_og_membership_label" } 
        ["node_title"]=> array(3) { 
            ["field"]=> string(5) "title" 
            ["table"]=> string(4) "node" 
            ["alias"]=> string(10) "node_title" } }

Is there is another way to implement a subquery in views?

Using Drupal 7 Views 3

share|improve this question

Also checkout http://drupal.org/sandbox/Fabianx/1184598 as this is adding a subquery to a view; which is exactly what your trying to do.

share|improve this answer

There are tons of pre_execute() examples here http://drupal.org/node/409808 that might help.

share|improve this answer

You will have to make db_select() query object (In Drupal 7 Views 3 you can not use simple SQL query any more). For further detail on "Dynamic queries" go to this link.



share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.