0

I would like to programmatically set an exposed filter for a field date (field_date) in a view.

Date has to be > of a specific date or between to dates.

I tried this, but it does not work. It ignores the filter.

$view = views_get_view('myview');
$view->set_display("default");
$view->get_exposed_input();

$exposed_filters["field_date_value_op "] = "between";

$exposed_filters["field_date_value"]["value"]["day"] = "";
$exposed_filters["field_date_value"]["value"]["month"] = "";
$exposed_filters["field_date_value"]["value"]["year"] = "";

$exposed_filters["field_date_value"]["min"]["day"] = 1;
$exposed_filters["field_date_value"]["min"]["month"] = 1;
$exposed_filters["field_date_value"]["min"]["year"] = 2014;

$exposed_filters["field_date_value"]["max"]["day"] = 1;
$exposed_filters["field_date_value"]["max"]["month"] = 12;
$exposed_filters["field_date_value"]["max"]["year"] = 2015;

$view->set_exposed_input($exposed_filters);
$view->execute();

Any idea? Thank you in advance.

  • In "Date has to be > of a specific date or between to dates. " You mean larger from a specific date between two dates? – JohnDoea Jan 7 '16 at 11:23
0

Use the below after set_display line.

function mymodule_views_pre_view(&$view) {
  if ($view->name == 'myview_user'){
    $view->get_exposed_input();
    dsm($view->exposed_input['created']['min']);
    $view->exposed_input['created']['min'] = "2015-01-01";
    $view->exposed_input['created']['max'] = "2016-01-01";

    //$view->set_exposed_input($exposed_filters);
    $view->execute();
  }
}

Please refer the example

  • thank you, but I'm sorry, I cannot understand how to use this in my case. display-name = default What I have to write instead of filter? and instead of filter_1_id? – lorenzo Jan 6 '16 at 19:49
  • filter_1_id would your filter or field id which you want to filter the data – Kalpan Jan 7 '16 at 4:55
  • that works in general, but not for the case I described – lorenzo Jan 7 '16 at 8:37
  • Please review my answer again. I have edited the answer with filters for Users table with created field. – Kalpan Jan 7 '16 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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