6

I am trying to alter views filters with hook_views_query_alter like

function custom_views_query_alter(&$view, &$query) {      
     foreach($query->where[1]['conditions'] as $idx => $condition) {
      dpr($condition); 
     } 
}

And below is the output I get from DPR function

Array
(
    [field] => node.status
    [value] => 1
    [operator] => =
)
Array
(
    [field] => node.type
    [value] => Array
        (
            [0] => pricing
        )

    [operator] => in
)
Array
(
    [field] => DatabaseCondition Object
        (
            [conditions:protected] => Array
                (
                    [#conjunction] => AND
                    [0] => Array
                        (
                            [field] => field_data_field_zip_range.field_zip_range_from
                            [value] => 65000
                            [operator] => <=
                        )

                    [1] => Array
                        (
                            [field] => field_data_field_zip_range.field_zip_range_to
                            [value] => 65000
                            [operator] => >=
                        )

                )

            [arguments:protected] => Array
                (
                )

            [changed:protected] => 1
            [queryPlaceholderIdentifier:protected] => 
        )

    [value] => 
    [operator] => 
)

So the first 2 filters are not a problem, you just edit the field and you set new values. Problem is the DatabaseCondition Object with this conditions:protected part. I just don't have a clue how to change values there.

There was similar questions here How to change the query value using hook_views_query_alter? but it was not solved. It only solves problems for first 2 filters and doesn't do anything for DatabaseCondition Object.

4
  • 5
    $conditions is protected but conditions() isn't...try $conditions = &$condition->conditions(); and manipulate them directly
    – Clive
    Mar 18, 2014 at 21:48
  • I just tried that in a way that I made foreach($query->where[1]['conditions'] as $idx => $condition) { $conditions = &$condition->conditions(); dpr($conditions); } And all I get is 500 Internal Server Error :(
    – Blissful
    Mar 19, 2014 at 5:46
  • @Clive - Thank You very much, this was exactly what I needed ! Jun 3, 2014 at 23:50
  • 1
    @MarkoBlazekovic HTTP 500 means "see server's error log for details". If you are still interested in this issue, post that details.
    – Mołot
    Dec 17, 2014 at 10:47

2 Answers 2

1

The conditions may be altered by getting them by reference:

function custom_views_query_alter(&$view, &$query) {
   $conditions = &$query->conditions();
   foreach($conditions as $idx => $condition) {
     if ($condition['field'] == 'node.type') {
       $conditions[$idx]['value'] = array("Some other type");
     }
   } 
}

Don't forget to modify the object/arrays by reference with the & character.

1
0

solutions above didn't work for me since $query->conditions() results in php error.

but here's how how i solved something similar:

i have a view which has a list/text filter (multivalue) which has default values. it is not exposed, but i change the values dynamically, depending on few $_GET values...

solution:

in hook_views_query_alter, $query->where[1]['conditions'][5]['field'] is a DatabaseCondition object--which i didn't manage to update, but i did override it's default value(s).

DatabaseCondition Object
(
[conditions:protected] => Array
    (
        [#conjunction] => OR
        [0] => Array
            (
                [field] => field_data_field_category_value_0.field_category_value
                [value] => apartment
                [operator] => =
            )
    )

[arguments:protected] => Array
    (
    )

[changed:protected] => 1
[queryPlaceholderIdentifier:protected] => 
)

what i did was to create a new DatabaseCondition and replace the old one:

<?php

// create db or
$db_or = db_or();
// add new condition
$db_or->condition('field_data_field_category_value_0.field_category_value', 'office', '=');
// assign new value
$query->where[1]['conditions'][5]['field'] = $db_or;
?>

also, you need to update $query->table_queue in order to match the new conditions. in my case:

<?php

$query->table_queue['field_data_field_category_value_0']['join']->extra[0]['value'] = 'office';

?>

of course, table alias, field name, and index (0 in my example) should match your settings...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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