1
votes

Drupal 7.26, Views 7.x-3.7

I have a view with exposed form filters, one of which is a taxonomy terms selection: you select one or many terms to display the nodes having that terms, or you select nothing and you have the complete node list.

The selection widget type is "Autocomplete", which means: when you type some text that doesn't exactly match a term name, you have an error. I can manage to hide the error (views config) but what I want is:

Suppose you have these terms in you vocabulary: "5 stars hotels", "4 stars hotels", "3 stars hotels" and "Private apartments"

When someone types "hotels" and hits submit (without selecting any specific term) I want to show results for any terms that contain the word "hotels" (in this case, the first 3 terms in my vocabulary)

I tried hook_query_alter but the $condition['field'] is a protected ConditionObject so I wasn't able to change it

function MYMODULE_views_query_alter($view, &$query) {
  if ( $view->name === "search" ) {
    foreach ($query->where as &$condition_group) {
      foreach ($condition_group['conditions'] as $conditionKey => &$condition) {
        if( is_object( $condition['field'] ) ){
          drupal_set_message($condition['field']);
        }
      }
    }
  }
}

OUTPUT:

(field_data_field_keywords_value_0.field_keywords_tid = :db_condition_placeholder_2)

Is there a way to act on the query at this level? Or any other approach to do so?

1

1 Answers

0
votes

Yes. Some options are here for example: https://drupal.stackexchange.com/questions/76651/how-to-use-hook-views-query-alter-to-modify-where-condition

The easiest would be to try something like this ( where you currently have drupal_set_message ):

$condition['field']->condition('field_data_field_keywords_value_0.field_keywords_tid like "%hotel%"' , '');

I'm actually not sure it's exactly correct :(

The documentation for condition: https://api.drupal.org/api/drupal/includes%21database%21query.inc/class/DatabaseCondition/7