3

I have created a customised module that export the content of a remote database tables to the view interface, every thing works fine except of the filter of the datetime field. In my file: mytable.views.inc

function mytable_views_data() {
  $data = array();
  $data['table1'] = array (
  'table' => 
   array (
    'group' => 'table1',
    'base' => 
     array (
      'field' => 'id',
      'title' => 'table1',
      'help' => 'table 1',
      'weight' => 10,
      'database' =>'database-name',
    ),
  ),
  'id' => 
   array (
    'title' => 'Id',
    'help' => 'Id',
    'field' => 
     array (
      'handler' => 'views_handler_field_numeric',
      'help' => 'Id',
      'click sortable' => true,
    ),
    'filter' => 
    array (
      'handler' => 'views_handler_filter_numeric',
      'allow empty' => true,
      'help' => 'Filter on <em class="placeholder">Id</em>',
    ),
    'argument' => 
    array (
      'handler' => 'views_handler_argument_numeric',
      'help' => 'Id',
    ),
    'sort' => 
    array (
      'handler' => 'views_handler_sort',
      'help' => 'Sort by <em class="placeholder">Id</em>',
    ),
  ),

     'date' => 
      array (
      'title' => 'Date',
      'help' => 'Datetime field',
      'field' => 
       array (
      'handler' => 'views_handler_field', // I tried also 'views_handler_field_date' and 'views_handler_field_datetime' but view displays the message field handler is broken
      'help' => 'Date',
      'click sortable' => true,
    ),
    'filter' => 
     array (
      'handler' => 'views_handler_filter_date',
      'allow empty' => true,
      'help' => 'Filter on <em class="placeholder">Date</em>',
    ),
    'argument' => 
    array (
      'handler' => 'views_handler_argument_date',
      'help' => 'Date',
    ),
    'sort' => 
    array (
      'handler' => 'views_handler_sort_date',
      'help' => 'Sort by <em class="placeholder">Date</em>',
    ),
  ),

);

return $data;
}

all the data fields are displayed properly in the view interface :enter image description here

and the filter of the field datetime is displayed also: enter image description here

The problem is the filter have no effect on the datetime field, I tried to filter the field but the filter has no effect and the data was not filtered, I think that there is a non compatibility issue between the field handler and the filter handler but once I tried to use the field handler: 'views_handler_field_date' or 'views_handler_field_datetime', the views didnt displayed the data of the datetime field and it has indicated that the field handler is broken, and with the handler 'views_handler_field' every things is displayed well but the filter is not working!

4
  • what did you put in the value in value type as in above picture.
    – madhurjya
    Dec 2, 2013 at 9:57
  • is between 2013-10-08 18:36:25 and 2013-10-08 18:36:30 Dec 2, 2013 at 9:59
  • Thank you but waiting for the answer I tried a solution and it is working :D I have added a customised filter to my module that can handel datetime fields Dec 2, 2013 at 10:25
  • Just for help, if any one find this must visit larsolesen.dk/node/273 Direct solution
    – Lucifer
    May 22, 2015 at 14:30

2 Answers 2

1

Try this handler views_handler_field_date, then uninstall and reinstall the module and try. Otherwise your changes will not affect.

0

To allow views_handler_field_date to work with SQL datetime format, you can try:

  • consider patch from Drupal issue (#2178287) which provides patch
  • extend views_handler_field_date by your own (see this post), e.g. by JulienThomas

    class module_handler_field_datetime extends views_handler_field_date {
     function render($values) {
         $value = $this->get_value($values);
    return parent::render(strtotime($value)); 
     }
    }
    

    then add this file into .info file:

    files[] = handlers/module_handler_field_datetime.inc
    

    or check custom filter provided in this blog (gist) as example:

    <?php
    /**
     * Custom filter handler for views, that handles DATETIME
     */
    class my_module_handler_filter_datetime extends views_handler_filter_date {
      function op_between($field) {
        if ($this->operator == 'between') {
          $a = intval(strtotime($this->value['min'], 0));
          $b = intval(strtotime($this->value['max'], 0));
        }
        else {
          $a = intval(strtotime($this->value['max'], 0));
          $b = intval(strtotime($this->value['min'], 0));
        }
        if ($this->value['type'] == 'offset') {
          // changed from original
          $a = (integer)time() + (integer)sprintf('%+d', $a); // keep sign
          $b = (integer)time() + (integer)sprintf('%+d', $b); // keep sign
          // changed from original ends
        }
        // %s is safe here because strtotime scrubbed the input and we might
        // have a string if using offset.
        $this->query->add_where_expression($this->options['group'], "$field >= '".date("Y-m-d H:i:s", $a)."'");
        $this->query->add_where_expression($this->options['group'], "$field <= '".date("Y-m-d H:i:s", $b)."'");
      }
      function op_simple($field) {
        $value = intval(strtotime($this->value['value'], 0));
        if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
          $this->query->add_where_expression($this->options['group'], "$field $this->operator DATE_ADD(NOW(), INTERVAL $value SECOND)");
        } else {
          $this->query->add_where_expression($this->options['group'], "$field $this->operator $value");
        }
      }
    }
    

    or how it's implemented in birthdays_field_views_handler_filter.inc.

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.