Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Form.php
Go to the documentation of this file.
1 <?php
13 
16 
22 class Form extends \Magento\Backend\Block\Widget\Form\Generic
23 {
24  const FORM_ELEMENT_ID = 'rate-form';
25 
29  protected $_titles = null;
30 
34  protected $_template = 'Magento_Tax::rate/form.phtml';
35 
41  protected $_taxData = null;
42 
46  protected $_fieldsetFactory;
47 
51  protected $_country;
52 
56  protected $_regionFactory;
57 
62 
67 
71  protected $_taxRateConverter;
72 
87  public function __construct(
88  \Magento\Backend\Block\Template\Context $context,
89  \Magento\Framework\Registry $registry,
90  \Magento\Framework\Data\FormFactory $formFactory,
91  \Magento\Directory\Model\RegionFactory $regionFactory,
92  \Magento\Directory\Model\Config\Source\Country $country,
93  \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory $fieldsetFactory,
94  \Magento\Tax\Helper\Data $taxData,
95  \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository,
96  \Magento\Tax\Model\TaxRateCollection $taxRateCollection,
97  \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter,
98  array $data = []
99  ) {
100  $this->_regionFactory = $regionFactory;
101  $this->_country = $country;
102  $this->_fieldsetFactory = $fieldsetFactory;
103  $this->_taxData = $taxData;
104  $this->_taxRateRepository = $taxRateRepository;
105  $this->_taxRateCollection = $taxRateCollection;
106  $this->_taxRateConverter = $taxRateConverter;
107  parent::__construct($context, $registry, $formFactory, $data);
108  }
109 
113  protected function _construct()
114  {
115  parent::_construct();
116  $this->setDestElementId(self::FORM_ELEMENT_ID);
117  }
118 
125  protected function _prepareForm()
126  {
127  $taxRateId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_ID);
128 
129  try {
130  if ($taxRateId) {
131  $taxRateDataObject = $this->_taxRateRepository->get($taxRateId);
132  }
133  } catch (NoSuchEntityException $e) {
134  /* tax rate not found */
135  }
136 
137  $sessionFormValues = (array)$this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_FORM_DATA);
138  $formData = isset($taxRateDataObject)
139  ? $this->_taxRateConverter->createArrayFromServiceObject($taxRateDataObject)
140  : [];
141  $formData = array_merge($formData, $sessionFormValues);
142 
143  if (isset($formData['zip_is_range']) && $formData['zip_is_range'] && !isset($formData['tax_postcode'])) {
144  $formData['tax_postcode'] = $formData['zip_from'] . '-' . $formData['zip_to'];
145  }
146 
148  $form = $this->_formFactory->create();
149 
150  $countries = $this->_country->toOptionArray(false, 'US');
151  unset($countries[0]);
152 
153  if (!isset($formData['tax_country_id'])) {
154  $formData['tax_country_id'] = $this->_scopeConfig->getValue(
155  \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
156  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
157  );
158  }
159 
160  if (!isset($formData['tax_region_id'])) {
161  $formData['tax_region_id'] = $this->_scopeConfig->getValue(
162  \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION,
163  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
164  );
165  }
166 
167  $regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter(
168  $formData['tax_country_id']
169  );
170 
171  $regions = $regionCollection->toOptionArray();
172  if ($regions) {
173  $regions[0]['label'] = '*';
174  } else {
175  $regions = [['value' => '', 'label' => '*']];
176  }
177 
178  $legend = $this->getShowLegend() ? __('Tax Rate Information') : '';
179  $fieldset = $form->addFieldset('base_fieldset', ['legend' => $legend, 'class' => 'form-inline']);
180 
181  if (isset($formData['tax_calculation_rate_id']) && $formData['tax_calculation_rate_id'] > 0) {
182  $fieldset->addField(
183  'tax_calculation_rate_id',
184  'hidden',
185  ['name' => 'tax_calculation_rate_id', 'value' => $formData['tax_calculation_rate_id']]
186  );
187  }
188 
189  $fieldset->addField(
190  'code',
191  'text',
192  [
193  'name' => 'code',
194  'label' => __('Tax Identifier'),
195  'title' => __('Tax Identifier'),
196  'class' => 'required-entry',
197  'required' => true
198  ]
199  );
200 
201  $fieldset->addField(
202  'zip_is_range',
203  'checkbox',
204  ['name' => 'zip_is_range', 'label' => __('Zip/Post is Range'), 'value' => '1']
205  );
206 
207  if (!isset($formData['tax_postcode'])) {
208  $formData['tax_postcode'] = $this->_scopeConfig->getValue(
209  \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_POSTCODE,
210  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
211  );
212  }
213 
214  $fieldset->addField(
215  'tax_postcode',
216  'text',
217  [
218  'name' => 'tax_postcode',
219  'label' => __('Zip/Post Code'),
220  'note' => __(
221  "'*' - matches any; 'xyz*' - matches any that begins on 'xyz' and are not longer than %1.",
222  $this->_taxData->getPostCodeSubStringLength()
223  )
224  ]
225  );
226 
227  $fieldset->addField(
228  'zip_from',
229  'text',
230  [
231  'name' => 'zip_from',
232  'label' => __('Range From'),
233  'required' => true,
234  'maxlength' => 9,
235  'class' => 'validate-digits',
236  'css_class' => 'hidden'
237  ]
238  );
239 
240  $fieldset->addField(
241  'zip_to',
242  'text',
243  [
244  'name' => 'zip_to',
245  'label' => __('Range To'),
246  'required' => true,
247  'maxlength' => 9,
248  'class' => 'validate-digits',
249  'css_class' => 'hidden'
250  ]
251  );
252 
253  $fieldset->addField(
254  'tax_region_id',
255  'select',
256  ['name' => 'tax_region_id', 'label' => __('State'), 'values' => $regions]
257  );
258 
259  $fieldset->addField(
260  'tax_country_id',
261  'select',
262  ['name' => 'tax_country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries]
263  );
264 
265  $fieldset->addField(
266  'rate',
267  'text',
268  [
269  'name' => 'rate',
270  'label' => __('Rate Percent'),
271  'title' => __('Rate Percent'),
272  'required' => true,
273  'class' => 'validate-not-negative-number'
274  ]
275  );
276 
277  $form->setAction($this->getUrl('tax/rate/save'));
278  $form->setUseContainer(true);
279  $form->setId(self::FORM_ELEMENT_ID);
280  $form->setMethod('post');
281 
282  if (!$this->_storeManager->hasSingleStore()) {
283  $form->addElement($this->_fieldsetFactory->create()->setLegend(__('Tax Titles')));
284  }
285 
286  if (isset($formData['zip_is_range']) && $formData['zip_is_range']) {
287  list($formData['zip_from'], $formData['zip_to']) = explode('-', $formData['tax_postcode']);
288  }
289  $form->setValues($formData);
290  $this->setForm($form);
291 
292  $this->setChild(
293  'form_after',
294  $this->getLayout()->createBlock(
295  \Magento\Framework\View\Element\Template::class
296  )->setTemplate('Magento_Tax::rate/js.phtml')
297  );
298 
299  return parent::_prepareForm();
300  }
301 }
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
__()
Definition: __.php:13
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Directory\Model\Config\Source\Country $country, \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory $fieldsetFactory, \Magento\Tax\Helper\Data $taxData, \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository, \Magento\Tax\Model\TaxRateCollection $taxRateCollection, \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter, array $data=[])
Definition: Form.php:87