Problem/Motivation
I have a Views table using Ajax with exposed filters, and it is rendered in a region below an Entityform form. The exposed filters include "contains" operators on textfields that may have no value.
If the Entityform is submitted and there are form validation errors, the View is rebuilt with an improper WHERE clause. For example, when the page is loaded I see this for the View:
WHERE (( (entityform.type IN ('book_log')) ))
... and when the Entityform is returned after failing validation, I see this:
WHERE (( (entityform.type IN ('book_log')) AND (field_collection_item_field_data_field_book__field_data_field_book_title.field_book_title_value LIKE '%%' ESCAPE '\\') AND (field_book_field_collection_item__field_data_field_reading_list_title.field_reading_list_title_value LIKE '%%' ESCAPE '\\') AND (profile_users__field_data_field_last_name.field_last_name_value LIKE '%%' ESCAPE '\\') ))
The problem is fields such as "book title" and "last name" are not required. Therefore any records without a value are going to be removed from the View when the LIKE is "'%%'". In some cases, I've seen situations where the entire View disappears after form submit because of this issue.
Proposed Resolution
The inner workings of Views exposed filters is somewhat outside of my knowledge. A quick fix would be to wrap op_contains in views_handler_filter_string.inc to ensure no empty values are ever passed. Would involve changing this:
function op_contains($field) {
$this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'LIKE');
}
... into this:
function op_contains($field) {
if (!empty($this->value)) {
$this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'LIKE');
}
}
However, I'm sure there is probably a better way to do this, most likely further upstream in processing to avoid a situation where $field is passed to op_contains with no value.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | views-empty_contains_operator-2483055-3.patch | 965 bytes | sgdev |
Comments
Comment #1
sgdev commentedComment #2
sgdev commentedI investigated a bit further and found a grouped exposed filter on a taxonomy term causing the same issue. After the entityform is submitted and returns due to validation errors, the View in a region below disappears because of the following
WHEREclause:I removed the grouped exposed filter, and the View does not have any further issues.
Comment #3
sgdev commentedHere is a patch that handles the proposed resolution.
I'd appreciate if others can review this, and let me know if there's a different way this should be approached. Thanks.
Comment #4
joelpittetThat seems like a straight forward fix
Comment #5
damienmckennaComment #7
damienmckennaCommitted. Thanks.