You are here

public function SimpleMathField::getValue in Views Simple Math Field 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/field/SimpleMathField.php \Drupal\views_simple_math_field\Plugin\views\field\SimpleMathField::getValue()

Throws

\Exception

Overrides FieldPluginBase::getValue

File

src/Plugin/views/field/SimpleMathField.php, line 384
Defines Drupal\views_simple_math_field\Plugin\views\field\SimpleMathField.

Class

SimpleMathField
Field handler to complete mathematical operation.

Namespace

Drupal\views_simple_math_field\Plugin\views\field

Code

public function getValue(ResultRow $values, $field = NULL) {
  parent::getValue($values, $field);
  $operation = $this->options['operation'];
  $entity = $this
    ->getEntity($values);
  $dataFieldOneValue = $this
    ->getFieldValue($values, $entity, TRUE);
  $dataFieldTwoValue = $this
    ->getFieldValue($values, $entity, FALSE);
  if ($operation === '+') {
    $value = $dataFieldOneValue + $dataFieldTwoValue;
  }
  elseif ($operation === '-') {
    $value = $dataFieldOneValue - $dataFieldTwoValue;
  }
  elseif ($operation === '*') {
    $value = $dataFieldOneValue * $dataFieldTwoValue;
  }
  elseif ($operation === '/') {
    $value = $dataFieldOneValue / $dataFieldTwoValue;
    if ($this->options['percentage'] === 1) {
      $value = $value * 100;
    }
  }
  elseif ($operation === '%') {
    $value = $dataFieldOneValue % $dataFieldTwoValue;
  }
  elseif ($operation === '**') {
    $value = pow($dataFieldOneValue, $dataFieldTwoValue);
  }
  else {
    $value = NULL;
  }
  return $value;
}