0

I've got content-type with two fields with real values. Their interior name are field_price1 and field_price2. I created page view includes contents. I've got search. I want to allow user to give some value(s) in search (min and max) to seek contents. I want to display only these contents whose field_price1 OR field_price2 is between min and max filled in by user.

Has someone any ideas?

I tried and I decided to create two filters. First filter (for field_price1) exposed, second (for field_price2) not. So that, in my search I've got two inputs (I chose between operator). I wanted the second filter inherits values from first filter. I coded:


function customSearch_views_pre_execute(&$view) {  
  if ($view->name == 'search') {        
    $filters = $view->display_handler->get_option('filters');   
    $filters['field_price2_value']['value']['min'] = $view->exposed_input['field_price1_value']['min'];
    $filters['field_price2_value']['value']['max'] = $view->exposed_input['field_price1_value']['max'];
    $view->display_handler->override_option('filters', $filters);
  }
}

I place that function in my custom module named customSearch. My view is named search. Unfornately it doesn't work. Second filter doesn't inherit values from first one. I don't know why. Could someone help me, please?

1 Answer 1

0

You will need to implement hook_views_query_alter to get this going.

Start with the filters in Views.

enter image description here

Make your filters look exactly like this. With that I mean that there should be two groups of filters with AND between the groups. And you need to set Or in the second group. This is important otherwise the code below will not work.

Then replace hook_views_pre_execute with the code below for hook_views_query_alter. I also recommend you rename your module to customsearch instead of customSearch, small letters is standard in function names for Drupal.

/**
* Implements hook_views_query_alter().
*/
function customsearch_views_query_alter(&$view, &$query) {
  if ($view->name == 'search') {
    if (isset($query->where[2]['conditions'][0]['field']) && $query->where[2]['conditions'][0]['field'] == 'field_data_field_price1.field_price1_value') {      
      $query->where[2]['conditions'][1]['value'][0] = $query->where[2]['conditions'][0]['value'][0];
      $query->where[2]['conditions'][1]['value'][1] = $query->where[2]['conditions'][0]['value'][1];
    }
    else {
      unset($query->where[2]);
    }
  }
}

In the code I first check to see if something has been entered in the exposed filter. If it is present we populate the second filter's values.

If you enable the module devel and do dsm($query) just before the last } you can debug and look in the array yourself. For exammple number 2 in $query->where[2] means the second group of filters, which you will see easily if you debug.

Update: I've added an else statement so that the price fields are not in effect at all if nothings been entered in the in betweens.

7
  • It works. Thank you so much. I' going to do a bit complicated modifications, yet I hope I can handle.
    – user19455
    Commented Aug 3, 2013 at 14:50
  • One problem appears. I added additional filter for field 'title' (I've got that field in my content-type). I added that filter to first group. When I fill in only input title, in the result there are no contents. But, when I fill in input title and input min price and max price we talk about, in the result are correct contents. I'd like to reach that after filling in only title, in the result appears some contents with matched title. Similarly, after filling only min price and max price. Could you help me?
    – user19455
    Commented Aug 3, 2013 at 15:04
  • Try creating a third group and put the title filter there. Use or between second and third filter group. Commented Aug 3, 2013 at 15:07
  • I created third group. When I marked OR operator between second and third filter group, it appears automatically OR operator between first and second filter group. Furthermore, if user fill in title, min price and max price, I'd like to display these contents whose title is appropriate (includes word given by user) and price is between min and max given by user. So, I want my result wll be more detailed.
    – user19455
    Commented Aug 3, 2013 at 15:13
  • I noticed that when I fill in only min price and max price it works, i. e. there are displayed contents whose price is between given value. Title is ignored. So that, it doesn't work if I fill in only title.
    – user19455
    Commented Aug 3, 2013 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.