0
votes

First time on stackoverflow so , i will try to be as clear as possible.

I'm using drupal 7 and views 3. I needed to create a custom views filter that handle dates range. So i looked at example and tried to mimic the behavior and i get some trouble. It seems that when i extend my own class from views_handler_filter , the query method is never invoked BUT if i extend my class from let's say views_handler_filter_string, it works oO...

I must forget something but i'm stuck here ... Here is my code, if someone can take a look and advise me about what happend , i would be very grateful.

Thanks everyone !

Here is my .views.inc file :

    <?php

        class v3d_date_custom_filter extends views_handler_filter {
          var $always_multiple = TRUE;

          function value_form(&$form, &$form_state) {
            //parent::value_form($form, $form_state);
            $form['value']['v3d_date']['period'] = array(
              '#type' => 'select',
              '#title' => 'Period',
              '#options' => array(
                '7_days' => 'Last 7 days',
                'yesterday' => 'Yesterday',
                'today' => 'Today',
                'custom' => 'Custom dates'),
              '#default_value' => 'custom',
              '#attributes' => array("onclick" => "period_click(this);"),
        );

        $form['value']['v3d_date']['start_date'] = array(
          '#type' => 'date_popup',
          '#date_format' => 'Y-m-d',
          '#title' => 'Start date',
          '#size' => 30);

        $form['value']['v3d_date']['end_date'] = array(
          '#type' => date_popup',
          '#title' => 'End date',
          '#date_format' => 'Y-m-d',
          '#size' => 30);
     }

     function exposed_validate(&$form, &$form_state) {

       if(is_null($form_state['values']['start_date']) &&
          is_null($form_state['values']['start_date'])) {
         return TRUE;
       }


  /*
   * If we get array for start_date or end_date
   * errors occured, but the date module will handle it.
   */
   if(!is_string($form_state['values']['start_date']) ||
      !is_string($form_state['values']['end_date'])) {
     return TRUE;
   }


       /* Get day, month and year from start_date string */
       if(!preg_match('/(\d+)-(\d+)-(\d+)/',
          $form_state['values']['start_date'],
           $start_date
        )) {
            return TRUE; }

        /* Get day, month and year from end_date string */
        if(!preg_match('/(\d+)-(\d+)-(\d+)/',
           $form_state['values']['end_date'],
           $end_date
        )) {
          return TRUE; }

        /* Create timestamps and compare */
        $start_date = mktime(0,0,0,$start_date[1],$start_date[2],$start_date[3]);
        $end_date = mktime(0,0,0,$end_date[1],$end_date[2],$end_date[3]);

        if($start_date >= $end_date) {
         form_set_error('start_date','Start date must be anterior to end date.');
       }
     }

     function query() {
       die('fdsfds');
       $this->ensure_my_table();
       $field = "$this->table_alias.$this->real_field";
       dsm($this);
     }

   }
?>

And my .module file

 <?php
function custom_filters_views_api() {
   return array(
           'api'=>3,
          'path' => drupal_get_path('module','custom_filters') . '/views',
        );
      }
?>

And part of my views_data that use my custom filter :

<?php                                                                                      

function voice_views_data() {

  $data['v_tp_voice']['date_utc_agent'] = array(
     'title' => t('date_utc_agent'),
     'help' => 'date_utc_agent',
     'field' => array('handler' => 'views_handler_field'),
     'filter' => array('handler' => 'v3d_date_custom_filter'),
     'sort' => array('handler' => 'views_handler_sort')
);
1

1 Answers

0
votes

I am working on this same problem. My setup looks very much like yours, though I do call parent::value_form($form, $form_state) in my value_form function.

I found that in the value_form function, I had to have something like

 $form['value'] = array(
'#type' => 'checkbox',
'#title' => "Filter by Date",
'#default_value' => $this->value,
);

in order to have my query() function called. Doing something like $form['value']['before'] = array( ... didn't work.