3

I am using hook_views_query_filter to append my own where clause condition with the where clause of the query generated by the view.

So for this I am using this hook.

Here is my code

   function mymodule_views_query_filter(&$view, $query) {
 if($view->name = "myview") {
 $query->where[1] = array(
            'clauses' => array(
                            '0'=>"field_test_value=0",

                             ),
            'args' => array('0' => 'test1', '1' => 'test2'),
            'type' => 'OR',
            );
}
print_r($query);
}

So the where clause becomes like this after print_r() funtion.

[where] => Array
    (
        [0] => Array
            (
                [clauses] => Array
                    (
                        [0] => node.type in ('%s')
                        [1] => node.status <> 0
                        [2] => term_data.name = '%s'
                        [3] => term_data_term_node__term_data.name = '%s'
                    )

                [args] => Array
                    (
                        [0] => test1
                        [1] => test2l
                        [2] => test3
                    )

                [type] => AND
            )

        [1] => Array
            (
                [clauses] => Array
                    (
                        [0] => field_test_value=0
                    )

                [args] => Array
                    (
                        [0] => test1
                        [1] => test2
                    )

                [type] => OR
            )

    )

Where the 0 index of the where clause is coming from the view query and the 1 index of the where is clause is coming from my code. But it only executes the 1 index of the where clause and display the results only if Where field_test_value=1 and I want something like this.

Where (node.type in ('%s') AND node.status <> 0 AND term_data.name = '%s' AND term_data_term_node__term_data.name = '%s') OR field_test_value=0
  • anyone who have idea about this problem? – Ahmad Jun 22 '12 at 5:06
0

You shouldn't alter the query objects properties directly. You need to use the methods it provides. In this case, you need to use add_where(). I think you want to consider something like this:

<?php
$clauses = array("field_test_value='%s'");
$args = array('test1');
$query->add_where(1, $clauses, $args);
$query->set_group_operator('OR'); // this is the operator that goes between WHERE groups
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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