RangeIntegerSprintfFormatter.php in Range 8
File
src/Plugin/Field/FieldFormatter/RangeIntegerSprintfFormatter.php
View source
<?php
namespace Drupal\range\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Form\FormStateInterface;
class RangeIntegerSprintfFormatter extends RangeFormatterBase {
public static function defaultSettings() {
return [
'format_string' => '%d',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['format_string'] = [
'#type' => 'textfield',
'#title' => $this
->t('Format'),
'#description' => $this
->t('See <a href=":url" target="_blank">PHP documentation</a> for a description of the format. <strong>Due to PHP limitations, a thousand separator cannot be used.</strong>', [
':url' => 'http://php.net/manual/en/function.sprintf.php',
]),
'#default_value' => $this
->getSetting('format_string'),
'#weight' => 10,
];
return $elements;
}
protected function formatNumber($number) {
return sprintf(FieldFilteredMarkup::create($this
->getSetting('format_string')), $number);
}
}