View source
<?php
namespace Drupal\currency\Plugin\views\field;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Amount extends FieldPluginBase implements ContainerFactoryPluginInterface {
protected $currencyStorage;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, RendererInterface $renderer, EntityStorageInterface $currency_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->definition += $this
->defaultDefinition();
$this->currencyStorage = $currency_storage;
$this->moduleHandler = $module_handler;
$this->renderer = $renderer;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('string_translation'), $container
->get('module_handler'), $container
->get('renderer'), $entity_type_manager
->getStorage('currency'));
}
protected function defaultDefinition() {
return array(
'currency_code' => 'XXX',
'currency_code_field' => NULL,
'currency_code_table' => NULL,
);
}
public function query() {
$this
->ensureMyTable();
if ($this->definition['currency_code_field']) {
$this
->addAdditionalFields(array(
'currency_code_field' => array(
'field' => $this->definition['currency_code_field'],
'table' => $this->definition['currency_code_table'] ? $this->definition['currency_code_table'] : $this->tableAlias,
),
));
}
parent::query();
}
public function defineOptions() {
$options = parent::defineOptions();
$options['currency_round'] = array(
'default' => FALSE,
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['currency_round'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Round amounts based on their currencies'),
'#default_value' => $this->options['currency_round'],
);
}
public function render(ResultRow $values) {
$currency = $this
->getCurrency($values);
$amount = $this
->getAmount($values);
return $currency
->formatAmount($amount, $this->options['currency_round']);
}
protected function getCurrency(ResultRow $values) {
$currency = NULL;
if ($this->definition['currency_code_field']) {
$currency_code = $this
->getValue($values, 'currency_code_field');
if ($currency_code) {
$currency = $this->currencyStorage
->load($currency_code);
}
}
if (!$currency && $this->definition['currency_code']) {
$currency = $this->currencyStorage
->load($this->definition['currency_code']);
}
if (!$currency) {
$currency = $this->currencyStorage
->load('XXX');
}
if (!$currency) {
throw new \RuntimeException('Could not load currency XXX.');
}
return $currency;
}
protected function getAmount(ResultRow $values) {
return $this
->getValue($values);
}
}