Problem/Motivation

Certain values in a Views "Search: Search term" filter can cause the below PHP warning:

Warning: Invalid argument supplied for foreach() in views_join->build_join() (line 1548 of ./sites/all/modules/views/includes/handlers.inc).

I believe this happens because the following declaration in modules/search.view.inc uses string "extra" data instead of arrays:

  $data['search_dataset']['table']['join'] = array(
    'node' => array(
      'left_table' => 'search_index',
      'left_field' => 'sid',
      'field' => 'sid',
      'extra' => 'search_index.type = search_dataset.type',
      'type' => 'INNER',
    ),
    'users' => array(
      'left_table' => 'search_index',
      'left_field' => 'sid',
      'field' => 'sid',
      'extra' => 'search_index.type = search_dataset.type',
      'type' => 'INNER',
    ),
  );

I am not sure exactly how the warning is triggered outside of the fact that I can reproduce it consistently only when the search terms include a string below the search string length threshold (default 3). E.g. searching for one two three four does not trigger the warning, but one two three fo does.

Proposed resolution

Refactor the above code to use arrays instead of strings for the extra data. I attempted to work out a patch for this, but it seems that the extra field is pretty rigid. A more advanced join method may need to be used to fix the issue.

Remaining tasks

Determine how resolve and submit patch.

User interface changes

None.

API changes

None.

Data model changes

None?

Comments

wells created an issue. See original summary.

ao2’s picture

This happens only in 7.x-3.16, right?

It should be caused by the change in commit 36eef6e, see also #1930288: views_join: wrong handling of $extra parameter.

Have you tried to just wrap the 'extra' values in an 'array()' declaration? I am not using D7 so I cannot try.

Jill L’s picture

I tested by changing the code to the following:

$data['search_dataset']['table']['join'] = array(
    'node' => array(
      'left_table' => 'search_index',
      'left_field' => 'sid',
      'field' => 'sid',
      'extra' => array('search_index.type' => 'search_dataset.type'),
      'type' => 'INNER',
    ),
    'users' => array(
      'left_table' => 'search_index',
      'left_field' => 'sid',
      'field' => 'sid',
      'extra' => array('search_index.type' => 'search_dataset.type'),
      'type' => 'INNER',
    ),
  );

which successfully stopped the error, but the search results have changed.

Before change:

Search terms: Families in Residence
Result: 1 Result
Search terms: Families Residence
Result 1 Result

After change:

Search terms: Families in Residence
Result: 0 Results
Search terms: Families Residence
Result 1 Result

In my case it's better to keep the search result when the keyword "in" is used, so I'm looking for another way to solve this.

ao2’s picture

Try just putting the string as is into an array, without converting it to an associative array.

Jill L’s picture

Status: Active » Needs review
FileSize
751 bytes

Yup, that worked! Now the search results are as expected, and the error is gone. Thanks.

Uploading patch in case others experience this issue.