What do you dedicate?
Drupal 6 bug: Advanced search using views
January 27, 2012
This post tries to explain the issue we detected when we were developing an advanced searcher using Views in Drupal 6


Nowadays, Drupal is one of the most used CMS by web developers. Maybe its the easy way to build a web application in few minutes using its own tools; or maybe its because developers can create custom modules and achieve their goals. There are many reasons that explain the success of Drupal. But, sometimes, even Drupal doesn't work as we expect.

 

We were thinking about to create an advanced searcher. There are several modules to get it, but we wanted to do it using the Views module. How? Easy: filtering contents.

 

Drupal gives us the chance to use some filters, and one of them is "Does not contains". This filter should avoid that contents which have these words (inserted on text field related with filter) are displayed. It seems really easy.

 

The function content was just a PHP line:
 

PHP Code $this->query->add_where($this->options['group'],"$upper($field) NOT LIKE $upper('%%%s%%')",$this->value);

 

It means that if we define a "Does not contain" operator, a new condition is always added to WHERE clause, even if the user doesn't use this operator on his/her searchings. The new condition should be added only if the user wanna use the filter "Does not contain"; but if the field is empty, Drupal must not add another condition.

 

To fix it, we modified the function:
 

PHP Code function op_not ($field, $upper) {

  if ($this->value != NULL) {
  $this->query->add_where($this->options['group'],"$upper($field) NOT LIKE $upper('%%%s%%')",$this->value);
  }
  else {
  $this->query->add_where($this->options['group'],"upper($field) IS NOT NULL");
  }

} //end of function

 


In this way, if the text field value is empty, a condition, that is always false, is added. So, the searching results won't be affected by this operator (as it shoul be!). If "Does not contains" text field contains anything, it works like before changes. So, now, we can use this filter correctly, being our Advanced Searcher more functional.

 

This issues was posted in Drupal.org, so we wish its gonna be included in the new version of View module.

Sdweb S.L. Santiago de Compostela - Barcelona