I use the Views API to programmatically build a view and execute it. To pass to this view the content of its exposed filters, I use the method view::set_exposed_input(), which simulates a user's choice of filter values.

Unfortunately, date_api_filter_handler does not respond to filter values if they are set in this manner. Other filter handlers I've used, and notably the ones bundled with the Views module, all respond correctly to values set using view::set_exposed_input(). In contrast, date_api_filter_handler behaves as if no filter value was passed, and therefore uses default filter values instead.

I've traced the problem to the date_api_filter_handler::init method, specifically the lines

    $this->force_value = FALSE;
    if (empty($this->options['exposed']) || (isset($this->options['expose']['identifier']) && !isset($_GET[$this->options['expose']['identifier']]))) {
      $this->force_value = TRUE;
    }

which explicitly check for $_GET variables instead of checking the view's exposed_filter attribute. Maybe the init method is called too early, but in any case the force_value flag later causes date_api_filter_handler::exposed_submit() to load default values, which causes the problem.

My workaround has been to add the filter values to $_GET itself, in addition to calling view::set_exposed_input(). However, I feel this is a hack and should be rectified in the Date API filter handler.

Comments

benkewell’s picture

Recently run into the same issue when trying to execute a view with exposed date filter by program.
Is there any way to solve this in the date filter code?

attiks’s picture

We have the same problem, for now we commented the line $this->force_value = TRUE; and for now all is still working.

Anyone knows any downsides to this solution?

tauno’s picture

Ran into the same problem but worse when using AJAX paging. Setting $_GET worked for the non-ajax first page view, but subsequent pages loaded with AJAX fail:
#934104: hook_views_pre_view called later when using ajax.

dman’s picture

THANKS for this workaround. I spent ages thinking I was getting something (like the format of the date argument) wrong.

Expected to work:

  // Basic view properties
  $view_name = 'tide_tables';
  $display_id = 'block_1';
  $view = views_get_view($view_name);

  ////////////////////////////////////////////////////
  // THE PROBLEM
  //
  $filters = array(
    'field_port_value_many_to_one' => $port,
    'date_filter' => array('value' => array('year' => $year)),
  );
  $view->set_exposed_input($filters);
  // ^^^^ failed.
  //
  $_GET = $filters;
  // ^^^^ fixes it!
  ////////////////////////////////////////////////////

  // ...
  // Run the view
  $view->set_display($display_id);
  $view->pre_execute();
  $view->execute();
  $results = $view->result;
  // etc

THANKS for showing a fix that lets me know I'm not doing it wrong!

develcuy’s picture

Version: 6.x-2.x-dev » 7.x-2.5

Same behavior in D7 with Views 3

muschpusch’s picture

Version: 7.x-2.5 » 7.x-2.6
Status: Active » Closed (works as designed)

it should be like this
$view->set_exposed_input(array('YOUR_IDENTIFIER' =>array('min' => $date, 'max' => $date2)));