You are here

public function Fraction::getValue in Fraction 8

Same name and namespace in other branches
  1. 2.x src/Plugin/views/field/Fraction.php \Drupal\fraction\Plugin\views\field\Fraction::getValue()

Loads the numerator and denominator values and converts to decimal.

Overrides FieldPluginBase::getValue

File

src/Plugin/views/field/Fraction.php, line 79

Class

Fraction
Field handler for Fraction database columns.

Namespace

Drupal\fraction\Plugin\views\field

Code

public function getValue(ResultRow $values, $field = NULL) {

  // Find the numerator and denominator field aliases.
  $numerator_alias = $this->aliases[$this->definition['additional fields']['numerator']];
  $denominator_alias = $this->aliases[$this->definition['additional fields']['denominator']];

  // If both values are available...
  if (isset($values->{$numerator_alias}) && isset($values->{$denominator_alias})) {

    // Convert to decimal.
    $numerator = $values->{$numerator_alias};
    $denominator = $values->{$denominator_alias};
    $precision = $this->options['precision'];
    $auto_precision = $this->options['auto_precision'];
    $fraction = new FractionClass($numerator, $denominator);
    return $fraction
      ->toDecimal($precision, $auto_precision);
  }
}