0

I've got content-type with two double-valued fields field_price1 and field_price2. There are interior names. I've also got page view (its machine name is search) displaying contents. I've got search with some filters. I decided to create one exposed filter connected with field_price1 field with between operator. Second filter isn't exposed and it is connected with field_price2 field.

I want to display content in the view only if its field_price1 or field_price2 is between values given by user in the search. So that, filter for field_price2 field has to inherit the value from the first exposed filter. I created custom module named customSearch with code posted below in customSearch.module file.

function customSearch_views_pre_view(&$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);
  }
}

Unforntunately, I've got errors.

Notice: Undefined property: view::$exposed_input in customSearch_views_pre_view() in line $filters['field_price2_value']['value']['min'] = $view->exposed_input['field_price1_value']['min'];

Notice: Undefined property: view::$exposed_input in customSearch_views_pre_view() in line `$filters['field_price2_value']['value']['max'] = $view->exposed_input['field_price1_value']['max'];

Could someone help me, please?

5
  • Did you expose the first filter? Aug 2, 2013 at 8:08
  • Yeah. In search I've got two additional inputs.
    – user19455
    Aug 2, 2013 at 8:09
  • What happens if you put 0 and 100 in the min and max exposed filter? Aug 2, 2013 at 8:10
  • These are the same errors.
    – user19455
    Aug 2, 2013 at 8:17
  • I don't know why I should code $filters['field_price2_value'] insted $filters['field_price2'] and why I code ['min'] and ['max'] - I never name such fields
    – user19455
    Aug 2, 2013 at 8:20

1 Answer 1

0

Try replacing

function customSearch_views_pre_view(&$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);
  }
}

with

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);
  }
}

And then clear your cache.

7
  • Ok, There are no errors, yet it doesn't work properly. It works for field_price1, but it doesn't work for field_price2. I fill inputs with appropriate values for field_price2 and in the result I don't receive any contents.
    – user19455
    Aug 2, 2013 at 8:39
  • Did you setup the OR groups? Aug 2, 2013 at 8:41
  • Yes, I've got two groups. In first I've got previous filters and operator AND. In second group I've got two filters we write about and opreator OR. I've got AND operator between groups.
    – user19455
    Aug 2, 2013 at 8:44
  • Try having just two groups. First group field_price1 and in the next group field_price2 and between the groups use OR. Aug 2, 2013 at 8:53
  • But what should I do with previous filters?
    – user19455
    Aug 2, 2013 at 8:56

Your Answer

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

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