|
|
Drupal example source code file (views_handler_field_math.inc)
The views_handler_field_math.inc Drupal example source code<?php // $Id: views_handler_field_math.inc,v 1.1.2.3 2010/11/05 07:20:54 dereine Exp $ /** * Render a mathematical expression as a numeric value * * Definition terms: * - float: If true this field contains a decimal value. If unset this field * will be assumed to be integer. * * @ingroup views_field_handlers */ class views_handler_field_math extends views_handler_field_numeric { function option_definition() { $options = parent::option_definition(); $options['expression'] = array('default' => ''); return $options; } function options_form(&$form, &$form_state) { $form['expression'] = array( '#type' => 'textarea', '#title' => t('Expression'), '#description' => t('Enter mathematical expressions such as 2 + 2 or sqrt(5). You my assign variables and create mathematical functions and evaluate them. Use the ; to separate these. For example: f(x) = x + 2; f(2).'), '#default_value' => $this->options['expression'], ); // Create a place for the help $form['expression_help'] = array(); parent::options_form($form, $form_state); // Then move the existing help: $form['expression_help'] = $form['alter']['help']; unset($form['expression_help']['#dependency']); unset($form['expression_help']['#process']); unset($form['alter']['help']); } function render($values) { ctools_include('math-expr'); $value = strtr($this->options['expression'], $this->get_render_tokens(array())); $expressions = explode(';', $value); $math = new ctools_math_expr; foreach ($expressions as $expression) { if ($expression !== '') { $value = $math->evaluate($expression); } } // The rest is directly from views_handler_field_numeric but because it // does not allow the value to be passed in, it is copied. if (!empty($this->options['set_precision'])) { $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']); } else { $remainder = abs($value) - intval(abs($value)); $value = $value > 0 ? floor($value) : ceil($value); $value = number_format($value, 0, '', $this->options['separator']); if ($remainder) { // The substr may not be locale safe. $value .= $this->options['decimal'] . substr($remainder, 2); } } // Check to see if hiding should happen before adding prefix and suffix. if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) { return ''; } return check_plain($this->options['prefix'] . $value . $this->options['suffix']); } function query() { } } Other Drupal examples (source code examples)Here is a short list of links related to this Drupal views_handler_field_math.inc source code file: |
"Drupal" is a registered trademark of Dries Buytaert.
my drupal tutorials and examples
Copyright
1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.
Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.