FractionFormatter.php in Fraction 8
File
src/Plugin/Field/FieldFormatter/FractionFormatter.php
View source
<?php
namespace Drupal\fraction\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class FractionFormatter extends FractionFormatterBase {
public static function defaultSettings() {
return [
'separator' => '/',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['separator'] = [
'#type' => 'textfield',
'#title' => $this
->t('Separator'),
'#description' => $this
->t('Specify the separator to display between the numerator and denominator.'),
'#default_value' => $this
->getSetting('separator'),
'#required' => TRUE,
'#weight' => 0,
];
return $elements;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$separator = $this
->getSetting('separator');
$summary[] = $this
->t('Separator: @separator', [
'@separator' => $separator,
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$separator = $this
->getSetting('separator');
foreach ($items as $delta => $item) {
$output = $item->fraction
->toString($separator);
$elements[$delta] = [
'#markup' => $this
->viewOutput($item, $output),
];
}
return $elements;
}
}