In Views, I would like to expose a fake field that is just an elementary mathematical expression of other fields of the table (let's say, for example, field1+field2).

I expose the field in hook_vies_data via some

  $data['my_table']['fake_field'] = array(
   'field' => array(
     'handler' => 'views_handler_my_handler',

Which function of the handler shall I define? And how do I get in the handler the two values of field1 and field2?

Alternatively, where do I found the documentation of what I am looking for?


2 Answers 2


Instead of creating own handler you should use already existent Global: math expression that allows you using of tokens to calculate everything you want.

  • The question was a bit simplified. The math expression I need is really more complicated, and can't be computed with the Global: math expression handler. I would just like to know where I find the documentation to write such a handler.
    – antonio
    Apr 12, 2013 at 5:53
  • I never see normal documentation about handlers. Just this one api.drupal.org/api/views/views.api.php/group/views_handlers/7 Apr 12, 2013 at 11:31

I've found this post that shows you how to create your custom views field handler for SQL expressions:


For my needs it's enough, but if you need complex data maipulation in PHP I think it's also a good point to start to work with.

But you need to create your custom filter and sort handlers to allow it working with expressions:

'filter' => array(
  'handler' => 'NumericExpressionFilterHandler',
  'allow empty' => TRUE,
'sort' => array(
  'handler' => 'NumericExpressionSortHandler',


class NumericExpressionFilterHandler extends views_handler_filter_numeric {
  function op_simple($field) {
    $placeholder = $this->placeholder();

      str_replace('.', '_', $field) . " $this->operator $placeholder",
      array($placeholder => $this->value['value']));

   * For simplicity limited to simple operations.
   * @return array
  public function operators() {
    $op = parent::operators();

    foreach ($op as $i => $operation) {
      if ($operation['method'] != 'op_simple') {

    return $op;



class NumericExpressionSortHandler extends views_handler_sort {
   * Called to add the sort to a query.
  function query() {
    // Add the field.
    $this->query->add_orderby(NULL, NULL, $this->options['order'], $this->table_alias . '_' . $this->real_field);



  • Please include a relevant quote of that hyperlink in your answer here. That will avoid your answer is perceived as a link-only answer (which may get deleted via moderation). Nov 19, 2016 at 11:05

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.