I am working with http://drupal.org/project/timefield and I want to get a field, sort, argument, and filter for a timefield's duration. This is simply a matter of adding an expression 'value2 - value' to the query, so it seems rather wasteful that it requires a whole new handler for each of sort, filter, and argument.

So I'm wondering: what if this were built into Views?

One way might be to provide a generic handler for each type, and have the hook_views_data() definition specify an expression like this:

 'mytable' => array(
  'my_expression_field' => array(
    'sort' => array(
      'handler' => 'views_handler_sort_expression',
      'expression' => 'value2 - value',

However, if instead of that we do this at the base class level for each type would mean that a definition in hook_views_data() could use any handler, eg views_handler_filter_in_operator. So:

 'mytable' => array(
  'my_expression_field' => array(
    'sort' => array(
      'handler' => 'views_handler_filter_in_operator',
      'expression' => 'value2 - value', // This is handled in the views_handler_filter class.

However, that would be a lot of reworking of the query() methods, or having the alias to the expression in $this->real_field to fool all the existing code which would be a bit of a DX WTF.

So not sure which approach is best. Maybe a) for now and save b) for Views 4?

Comments

dawehner’s picture

Having the need to rewrite every query method just for adding a feature which can be done using a custom handler is from my perspective not a worth way to spend time. Writing custom handlers for this is a much smaller effort, at least from my perspective.

Changing this would maybe also break some query altering code, so that's definitive not 3.x stuff.
Beside this you have to look at the maintainability of the code.

joachim’s picture

Ok I'm now definitely leaning towards option b as it seems like there's currently no way to aggregate on an expression.