View source
<?php
namespace Drupal\commerce_price\Plugin\Field\FieldFormatter;
use CommerceGuys\Intl\Formatter\CurrencyFormatterInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PriceDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $currencyFormatter;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, CurrencyFormatterInterface $currency_formatter) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->currencyFormatter = $currency_formatter;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('commerce_price.currency_formatter'));
}
public static function defaultSettings() {
return [
'strip_trailing_zeroes' => FALSE,
'currency_display' => 'symbol',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = [];
$elements['strip_trailing_zeroes'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Strip trailing zeroes after the decimal point.'),
'#default_value' => $this
->getSetting('strip_trailing_zeroes'),
];
$elements['currency_display'] = [
'#type' => 'radios',
'#title' => $this
->t('Currency display'),
'#options' => [
'symbol' => $this
->t('Symbol (e.g. "$")'),
'code' => $this
->t('Currency code (e.g. "USD")'),
'none' => $this
->t('None'),
],
'#default_value' => $this
->getSetting('currency_display'),
];
return $elements;
}
public function settingsSummary() {
$summary = [];
if ($this
->getSetting('strip_trailing_zeroes')) {
$summary[] = $this
->t('Strip trailing zeroes after the decimal point.');
}
else {
$summary[] = $this
->t('Do not strip trailing zeroes after the decimal point.');
}
$currency_display = $this
->getSetting('currency_display');
$currency_display_options = [
'symbol' => $this
->t('Symbol (e.g. "$")'),
'code' => $this
->t('Currency code (e.g. "USD")'),
'none' => $this
->t('None'),
];
$summary[] = $this
->t('Currency display: @currency_display.', [
'@currency_display' => $currency_display_options[$currency_display],
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$options = $this
->getFormattingOptions();
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => $this->currencyFormatter
->format($item->number, $item->currency_code, $options),
'#cache' => [
'contexts' => [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'country',
],
],
];
}
return $elements;
}
protected function getFormattingOptions() {
$options = [
'currency_display' => $this
->getSetting('currency_display'),
];
if ($this
->getSetting('strip_trailing_zeroes')) {
$options['minimum_fraction_digits'] = 0;
}
return $options;
}
}