9

I'm using 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 got 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.

I must forget something but I'm stuck here.

Here is my code, if someone can take a look and advise me about what happened, I would be very grateful.

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')
  );

  return $data;
}
5
  • To which field your're mapping the custom filter(Field api or custom table). Commented Oct 15, 2013 at 12:26
  • 1
    it's an sql view(v_tp_voice) taken from an external database called equalone. so i'm mapping on a custom table.
    – d4rkfl4sh
    Commented Oct 15, 2013 at 12:38
  • 1
    Don't know how that could provoke the described effect, but I always thought that the filter class must be in a separate file and being referenced in your .info file too. It's not clear from the question whether that is the case.
    – berliner
    Commented Nov 26, 2013 at 3:35
  • yes you have to extend ! (aka what's your problem ?)
    – rémy
    Commented Dec 5, 2013 at 21:44
  • Why not use the date field module? It has a sub-module integration with views. I am not sure that it will do everything you want, but it might be worth a try.
    – todinov
    Commented Dec 16, 2014 at 9:49

1 Answer 1

1

As suggested by todinov, you can use Drupal contrib modules Date along with its sub module Date Views which will most probably handle any kind of filter in Views. If it doesn't help, you can achieve it through code by implementing hook_views_query_alter.

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.