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.

Comments

sgdev’s picture

Issue summary: View changes
sgdev’s picture

I 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 WHERE clause:

WHERE (( (entityform.type IN  ('book_log')) AND (taxonomy_term_data_taxonomy_term_hierarchy__field_data_field_short_name.field_short_name_value LIKE '' ESCAPE '\\') ))

I removed the grouped exposed filter, and the View does not have any further issues.

sgdev’s picture

Status: Active » Needs review
StatusFileSize
new965 bytes

Here 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.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

That seems like a straight forward fix

damienmckenna’s picture

  • DamienMcKenna committed 04f4fce on 7.x-3.x authored by ron_s
    Issue #2483055 by ron_s, joelpittet: Empty "contains" operator causes...
damienmckenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.