PercentageFormatter.php in Fraction 8
File
src/Plugin/Field/FieldFormatter/PercentageFormatter.php
View source
<?php
namespace Drupal\fraction\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\fraction\Fraction;
class PercentageFormatter extends FractionDecimalFormatter {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$percentage = clone $item->fraction;
$percentage
->multiply(Fraction::createFromDecimal('100'));
$auto_precision = !empty($this
->getSetting('auto_precision'));
$output = $percentage
->toDecimal($this
->getSetting('precision'), $auto_precision) . '%';
$elements[$delta] = [
'#markup' => $this
->viewOutput($item, $output),
];
}
return $elements;
}
}