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.

1
  • In "Date has to be > of a specific date or between to dates. " You mean larger from a specific date between two dates?
    – user16289
    Commented Jan 7, 2016 at 11:23

1 Answer 1

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

4
  • 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
    Commented Jan 6, 2016 at 19:49
  • filter_1_id would your filter or field id which you want to filter the data
    – Kalpan
    Commented Jan 7, 2016 at 4:55
  • that works in general, but not for the case I described
    – lorenzo
    Commented Jan 7, 2016 at 8:37
  • Please review my answer again. I have edited the answer with filters for Users table with created field.
    – Kalpan
    Commented Jan 7, 2016 at 10:25

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.