2

Would there happen to be a way to alter the conditions in a DatabaseCondition object? Say I have this:

DatabaseCondition Object
(
    [conditions:protected] => Array
        (
            [#conjunction] => AND
            [0] => Array
                (
                    [field] => search_index.type
                    [value] => node
                    [operator] => =
                )

            [1] => Array
                (
                    [field] => DatabaseCondition Object
                        (
                            [conditions:protected] => Array
                                (
                                    [#conjunction] => OR
                                    [0] => Array
                                        (
                                            [field] => search_index.word
                                            [value] => foo
                                            [operator] => =
                                        )

                                    [1] => Array
                                        (
                                            [field] => search_index.word
                                            [value] => bar
                                            [operator] => =
                                        )

                                )

                            [arguments:protected] => Array
                                (
                                )

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

                    [value] => 
                    [operator] => IS NULL
                )

        )

    [arguments:protected] => Array
        (
        )

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

I would like to make changes on the conditions. Any ideas?

2 Answers 2

4

You can grab the conditions array for the full query like this:

$conditions =& $query->conditions();

As you're getting the array by reference any changes you make to it (and the conditions within) will be reflected back on the query object before it's executed.

If you want to replace all of the conditions in the query you can simply overwrite that array with a new one (filled with conditions obviously).

It might be safer, though, to loop through the conditions array and remove/alter only the conditions that you need to change. This will make sure that conditions added by other modules will still get their chance to be included in the query.

Editing an existing condition is tricky (I haven't managed to get it to work yet though I haven't tried very hard!). I think your best bet is to rebuild the conditions you need to change from scratch and replace them rather than try to edit them directly.

1

You could try DatabaseCondition::condition($field, $value, $operator), which adds another item below your bar value. But I think that it won't work for you, beacuse there could be another problem with the $field value.

This is the same problem preventing you from adding a new DatabaseCondition object to $view->query->where too.

I figured out that the $field value is a refference to an actual field within fields section of the $view object. Have e.g. a taxonomy tags field and a view filter that shows nodes only with both terms foo AND bar.

This will result in:

  • adding two (hidden) fields into your query - called sth. like: field_term_tags_1 and field_term_tags_2 (simplified)
  • adding a DatabaseCondition object with two conditions like: field_term_tags_1|foo|= and field_term_tags_2|bar|=

So the final SQL will look sth. like:

SELECT ..., field_term_tags_1, field_term_tags_2 
FROM ... 
WHERE field_term_tags_1 = 'foo' AND field_term_tags_2 = 'bar' ...

So in this case I'd recommend you to use your own PHP filtering in hook_views_pre_render() (after the query has been made) rather than messing with the $view object.

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.