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
8 
17 class Form extends \Magento\Backend\Block\Widget\Form\Generic
18 {
24  protected $_reportTypeOptions = [];
25 
31  protected $_fieldVisibility = [];
32 
38  protected $_fieldOptions = [];
39 
49  public function setFieldVisibility($fieldId, $visibility)
50  {
51  $this->_fieldVisibility[$fieldId] = (bool)$visibility;
52  }
53 
62  public function getFieldVisibility($fieldId, $defaultVisibility = true)
63  {
64  if (!array_key_exists($fieldId, $this->_fieldVisibility)) {
65  return $defaultVisibility;
66  }
67  return $this->_fieldVisibility[$fieldId];
68  }
69 
79  public function setFieldOption($fieldId, $option, $value = null)
80  {
81  if (is_array($option)) {
82  $options = $option;
83  } else {
84  $options = [$option => $value];
85  }
86  if (!array_key_exists($fieldId, $this->_fieldOptions)) {
87  $this->_fieldOptions[$fieldId] = [];
88  }
89  foreach ($options as $k => $v) {
90  $this->_fieldOptions[$fieldId][$k] = $v;
91  }
92  }
93 
102  public function addReportTypeOption($key, $value)
103  {
104  $this->_reportTypeOptions[$key] = __($value);
105  return $this;
106  }
107 
113  protected function _prepareForm()
114  {
115  $actionUrl = $this->getUrl('*/*/sales');
116 
118  $form = $this->_formFactory->create(
119  [
120  'data' => [
121  'id' => 'filter_form',
122  'action' => $actionUrl,
123  'method' => 'get'
124  ]
125  ]
126  );
127 
128  $htmlIdPrefix = 'sales_report_';
129  $form->setHtmlIdPrefix($htmlIdPrefix);
130  $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Filter')]);
131 
132  $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
133 
134  $fieldset->addField('store_ids', 'hidden', ['name' => 'store_ids']);
135 
136  $fieldset->addField(
137  'report_type',
138  'select',
139  [
140  'name' => 'report_type',
141  'options' => $this->_reportTypeOptions,
142  'label' => __('Date Used')
143  ]
144  );
145 
146  $fieldset->addField(
147  'period_type',
148  'select',
149  [
150  'name' => 'period_type',
151  'options' => ['day' => __('Day'), 'month' => __('Month'), 'year' => __('Year')],
152  'label' => __('Period'),
153  'title' => __('Period')
154  ]
155  );
156 
157  $fieldset->addField(
158  'from',
159  'date',
160  [
161  'name' => 'from',
162  'date_format' => $dateFormat,
163  'label' => __('From'),
164  'title' => __('From'),
165  'required' => true,
166  'css_class' => 'admin__field-small',
167  'class' => 'admin__control-text'
168  ]
169  );
170 
171  $fieldset->addField(
172  'to',
173  'date',
174  [
175  'name' => 'to',
176  'date_format' => $dateFormat,
177  'label' => __('To'),
178  'title' => __('To'),
179  'required' => true,
180  'css_class' => 'admin__field-small',
181  'class' => 'admin__control-text'
182  ]
183  );
184 
185  $fieldset->addField(
186  'show_empty_rows',
187  'select',
188  [
189  'name' => 'show_empty_rows',
190  'options' => ['1' => __('Yes'), '0' => __('No')],
191  'label' => __('Empty Rows'),
192  'title' => __('Empty Rows')
193  ]
194  );
195 
196  $form->setUseContainer(true);
197  $this->setForm($form);
198 
199  return parent::_prepareForm();
200  }
201 
208  protected function _initFormValues()
209  {
210  $data = $this->getFilterData()->getData();
211  foreach ($data as $key => $value) {
212  if (is_array($value) && isset($value[0])) {
213  $data[$key] = explode(',', $value[0]);
214  }
215  }
216  $this->getForm()->addValues($data);
217  return parent::_initFormValues();
218  }
219 
225  protected function _beforeToHtml()
226  {
227  $result = parent::_beforeToHtml();
228 
230  $fieldset = $this->getForm()->getElement('base_fieldset');
231 
232  if (is_object($fieldset) && $fieldset instanceof \Magento\Framework\Data\Form\Element\Fieldset) {
233  // apply field visibility
234  foreach ($fieldset->getElements() as $field) {
235  if (!$this->getFieldVisibility($field->getId())) {
236  $fieldset->removeField($field->getId());
237  }
238  }
239  // apply field options
240  foreach ($this->_fieldOptions as $fieldId => $fieldOptions) {
241  $field = $fieldset->getElements()->searchById($fieldId);
243  if ($field) {
244  foreach ($fieldOptions as $k => $v) {
245  $field->setDataUsingMethod($k, $v);
246  }
247  }
248  }
249  }
250 
251  return $result;
252  }
253 }
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
__()
Definition: __.php:13
getFieldVisibility($fieldId, $defaultVisibility=true)
Definition: Form.php:62
$fieldId
Definition: element.phtml:16
$value
Definition: gender.phtml:16
setFieldVisibility($fieldId, $visibility)
Definition: Form.php:49
setFieldOption($fieldId, $option, $value=null)
Definition: Form.php:79