RadioactivityReferenceValue.php in Radioactivity 4.0.x
File
src/Plugin/Field/FieldFormatter/RadioactivityReferenceValue.php
View source
<?php
namespace Drupal\radioactivity\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class RadioactivityReferenceValue extends RadioactivityReferenceFormatterBase {
public static function defaultSettings() {
return [
'decimals' => 0,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
return [
'decimals' => [
'#title' => $this
->t('Decimals'),
'#type' => 'number',
'#min' => 0,
'#required' => TRUE,
'#description' => $this
->t('The number of decimals to show.'),
'#default_value' => $this
->getSetting('decimals'),
],
] + parent::settingsForm($form, $form_state);
}
public function settingsSummary() {
$summary = [];
$summary[] = t('Decimals: @number', [
'@number' => $this
->getSetting('decimals'),
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
if (empty($entity)) {
continue;
}
$elements[$delta] = [
'#markup' => number_format($entity
->getEnergy(), $this
->getSetting('decimals')),
'#cache' => [
'tags' => $entity
->getCacheTags(),
],
];
}
return $elements;
}
}