Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tax.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Currency;
10 
11 class Tax extends \Magento\Framework\Data\Form\Element\AbstractElement
12 {
13 
17  protected $storeManager;
18 
22  protected $localeCurrency;
23 
32  public function __construct(
33  \Magento\Framework\Data\Form\Element\Factory $factoryElement,
34  \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
35  \Magento\Framework\Escaper $escaper,
36  \Magento\Store\Model\StoreManagerInterface $storeManager,
37  \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
38  array $data = []
39  ) {
40  $this->localeCurrency = $localeCurrency;
41  $this->storeManager = $storeManager;
42  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
43  }
44 
48  protected function _construct()
49  {
50  parent::_construct();
51  }
52 
57  protected function getStore($attribute)
58  {
59  if (!($storeId = $attribute->getStoreId())) {
60  $storeId = $this->getForm()->getDataObject()->getStoreId();
61  }
62  $store = $this->storeManager->getStore($storeId);
63  return $store;
64  }
65 
71  public function getEscapedValue($index = null)
72  {
73  $values = $this->getValue();
74 
75  if (!is_array($values)) {
76  return null;
77  }
78 
79  foreach ($values as $key => $value) {
80  $price = array_key_exists('price', $value) ? $value['price'] : $value['value'];
81  try {
82  if ($attribute = $this->getEntityAttribute()) {
83  $store = $this->getStore($attribute);
84  $currency = $this->localeCurrency->getCurrency($store->getBaseCurrencyCode());
85 
86  $values[$key]['value'] = $currency->toCurrency($price, ['display' => Currency::NO_SYMBOL]);
87  } else {
88  // default format: 1234.56
89  $values[$key]['value'] = number_format($price, 2, null, '');
90  }
91  } catch (\Exception $e) {
92  $values[$key]['value'] = $price;
93  }
94  }
95 
96  return $values;
97  }
98 }
$values
Definition: options.phtml:88
$price
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Data\Form\Element\Factory $factoryElement, \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection, \Magento\Framework\Escaper $escaper, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Locale\CurrencyInterface $localeCurrency, array $data=[])
Definition: Tax.php:32
const NO_SYMBOL
Definition: Currency.php:40
$index
Definition: list.phtml:44