1

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?

1

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 '13 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 – Oleg Sherbakov Apr 12 '13 at 11:31
0

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

http://tech-tamer.com/drupal-7-using-sql-expressions-in-views-3/

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',
),
[...]

NumericExpressionFilterHandler.php:

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

    $this->query->add_having_expression($this->options['group'],
      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') {
        unset($op[$i]);
      }
    }

    return $op;
  }

}

NumericExpressionSortHandler.php

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

}

Regards.

  • 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). – Pierre.Vriens Nov 19 '16 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.