RadioactivityValue.php in Radioactivity 8.3
File
src/Plugin/Field/FieldFormatter/RadioactivityValue.php
View source
<?php
namespace Drupal\radioactivity\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
class RadioactivityValue extends FormatterBase {
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 ($items as $delta => $item) {
if (!$item
->isEmpty()) {
$elements[$delta] = [
'#markup' => $this
->viewValue($item->energy),
];
}
}
return $elements;
}
protected function viewValue($energy) {
return number_format($energy, $this
->getSetting('decimals'));
}
}