1
votes

I am trying to get data from a view I have created. I have exposed nid and another field called uid and they are working fine when I apply filter on them. However there is another exposed filter "field_date" which is working with the ui of drupal but not programmatically in my custome module. This is a datestamp field. My code sample is as below. The value passed is in unix timestamp format.

    views_include('view');
    $name = Rest::$DAILY_LOGS_VIEW;
    $display = Rest::$DAILY_LOGS_VIEW_PAGE;

    $alter = array('exposed' =>array('field_uid_value' => $uid));
    if(!empty($nid)) $alter['exposed']['nid'] = (int) $nid;

    //if(!empty($from_date)) $alter['exposed']['field_date_value_1']['min'] = (int) $from_date;
    //if(!empty($to_date)) $alter['exposed']['field_date_value_1']['max'] = (int) $to_date;
    //if(!empty($to_date)) $alter['exposed']['field_date_value_1[max]'] = (int) $to_date;

    if(!empty($date)) $alter['exposed']['field_date_value'] = (int) $date;

    $view = views_get_view($name);
    $view->init_display($display);
    $view->preview=TRUE;
    $view->is_cacheable = FALSE;

    if(isset($alter['exposed'])) {
      foreach($alter['exposed'] as $key => $valor) {
         $view->exposed_input[$key] = $valor;
      }
    }

    $view->init_pager();
    $view->pager['items_per_page']               = $count;
    $view->pager['use_pager']                    = true;
    $view->display_handler->options['use_pager'] = true;
    $view->set_items_per_page($count);
    $view->pager['current_page'] = $page;

    $view->pre_execute();
    $output = $view->display_handler->preview();
    $view->post_execute();

    return $view->result;

So I have two questions here.

What is the correct format to give for filters with date format?

What is the format to give for filters with date "between" values i.e min and max values?

2

2 Answers

1
votes

I had the same problem and managed to fix it using this:

$view->exposed_input[$key]["value"]["month"] = $month;
$view->exposed_input[$key]["value"]["year"] = $year;

Not sure if you need to have the "Day" as well but I had selected the "Month" on granularity

1
votes

Answer to the second part: Format to give for filters with date "between" values i.e min and max values:

I installed the date module and gave the filters as

if(!empty($from_date)) $view->exposed_input['field_date_value']['min']['date']= $from_date;
if(!empty($to_date)) $view->exposed_input['field_date_value']['max']['date']= $to_date;