Currency.php in Currency 8.3
File
src/Plugin/views/field/Currency.php
View source
<?php
namespace Drupal\currency\Plugin\views\field;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\currency\Entity\CurrencyInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Currency extends FieldPluginBase {
protected $currencyStorage;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, TranslationInterface $string_translation, EntityStorageInterface $currency_storage) {
if (!isset($configuration['currency_method'])) {
throw new \InvalidArgumentException('Missing currency_method definition.');
}
elseif (!method_exists(CurrencyInterface::class, $configuration['currency_method'])) {
throw new \InvalidArgumentException(sprintf('Method %s does not exist on \\Drupal\\currency\\Entity\\CurrencyInterface.', $configuration['currency_method']));
}
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currencyStorage = $currency_storage;
$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'), $entity_type_manager
->getStorage('currency'));
}
public function render(ResultRow $values) {
$currency_code = $this
->getValue($values);
$currency = $this->currencyStorage
->load($currency_code);
if ($currency) {
return call_user_func([
$currency,
$this->configuration['currency_method'],
]);
}
else {
return $this
->t('Unknuwn currency %currency_code', array(
'%currency_code' => $currency_code,
));
}
}
}
Classes
Name |
Description |
Currency |
A Views field handler to get properties from currencies. |