PriceTwigExtension.php in Price 3.0.x
File
src/TwigExtension/PriceTwigExtension.php
View source
<?php
namespace Drupal\price\TwigExtension;
use Drupal\price\Price;
class PriceTwigExtension extends \Twig_Extension {
public function getFilters() {
return [
new \Twig_SimpleFilter('price_format', [
$this,
'formatPrice',
]),
];
}
public function getName() {
return 'price.twig_extension';
}
public static function formatPrice($price) {
if (empty($price)) {
return '';
}
if ($price instanceof Price) {
$price = $price
->toArray();
}
if (is_array($price) && isset($price['currency_code']) && isset($price['number'])) {
$currency_formatter = \Drupal::service('price.currency_formatter');
return $currency_formatter
->format($price['number'], $price['currency_code']);
}
else {
throw new \InvalidArgumentException('The "price_format" filter must be given a price object or an array with "number" and "currency_code" keys.');
}
}
}