Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractForm.php
Go to the documentation of this file.
1 <?php
7 
14 
21 {
27  protected $_elements;
28 
34  protected $_types = [];
35 
39  protected $_factoryElement;
40 
45 
49  protected $customAttributes = [];
50 
56  public function __construct(Factory $factoryElement, CollectionFactory $factoryCollection, $data = [])
57  {
58  $this->_factoryElement = $factoryElement;
59  $this->_factoryCollection = $factoryCollection;
60  parent::__construct($data);
61  $this->_construct();
62  }
63 
71  protected function _construct()
72  {
73  }
74 
82  public function addType($type, $className)
83  {
84  $this->_types[$type] = $className;
85  return $this;
86  }
87 
93  public function getElements()
94  {
95  if (empty($this->_elements)) {
96  $this->_elements = $this->_factoryCollection->create(['container' => $this]);
97  }
98  return $this->_elements;
99  }
100 
108  public function setReadonly($readonly, $useDisabled = false)
109  {
110  if ($useDisabled) {
111  $this->setDisabled($readonly);
112  $this->setData('readonly_disabled', $readonly);
113  } else {
114  $this->setData('readonly', $readonly);
115  }
116  foreach ($this->getElements() as $element) {
117  $element->setReadonly($readonly, $useDisabled);
118  }
119 
120  return $this;
121  }
122 
130  public function addElement(AbstractElement $element, $after = null)
131  {
132  $element->setForm($this);
133  $this->getElements()->add($element, $after);
134  return $this;
135  }
136 
150  public function addField($elementId, $type, $config, $after = false)
151  {
152  if (isset($this->_types[$type])) {
153  $type = $this->_types[$type];
154  }
155  $element = $this->_factoryElement->create($type, ['data' => $config]);
156  $element->setId($elementId);
157  $this->addElement($element, $after);
158  return $element;
159  }
160 
167  public function removeField($elementId)
168  {
169  $this->getElements()->remove($elementId);
170  return $this;
171  }
172 
182  public function addFieldset($elementId, $config, $after = false, $isAdvanced = false)
183  {
184  $element = $this->_factoryElement->create('fieldset', ['data' => $config]);
185  $element->setId($elementId);
186  $element->setAdvanced($isAdvanced);
187  $this->addElement($element, $after);
188  return $element;
189  }
190 
198  public function addColumn($elementId, $config)
199  {
200  $element = $this->_factoryElement->create('column', ['data' => $config]);
201  $element->setForm($this)->setId($elementId);
202  $this->addElement($element);
203  return $element;
204  }
205 
213  public function convertToArray(array $arrAttributes = [])
214  {
215  $res = [];
216  $res['config'] = $this->getData();
217  $res['formElements'] = [];
218  foreach ($this->getElements() as $element) {
219  $res['formElements'][] = $element->toArray();
220  }
221  return $res;
222  }
223 
231  public function addCustomAttribute($key, $value)
232  {
233  $this->customAttributes[$key] = $value;
234  return $this;
235  }
236 
246  public function serialize($keys = [], $valueSeparator = '=', $fieldSeparator = ' ', $quote = '"')
247  {
248  $data = [];
249  if (empty($keys)) {
250  $keys = array_keys($this->_data);
251  }
252 
253  $customAttributes = array_filter($this->customAttributes);
254  $keys = array_merge($keys, array_keys(array_diff($this->customAttributes, $customAttributes)));
255 
256  foreach ($this->_data as $key => $value) {
257  if (in_array($key, $keys)) {
258  $data[] = $key . $valueSeparator . $quote . $value . $quote;
259  }
260  }
261 
262  foreach ($customAttributes as $key => $value) {
263  $data[] = $key . $valueSeparator . $quote . $value . $quote;
264  }
265 
266  return implode($fieldSeparator, $data);
267  }
268 }
convertToArray(array $arrAttributes=[])
getData($key='', $index=null)
Definition: DataObject.php:119
$config
Definition: fraud_order.php:17
$quote
addElement(AbstractElement $element, $after=null)
addField($elementId, $type, $config, $after=false)
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
serialize($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
setReadonly($readonly, $useDisabled=false)
setData($key, $value=null)
Definition: DataObject.php:72
__construct(Factory $factoryElement, CollectionFactory $factoryCollection, $data=[])
addFieldset($elementId, $config, $after=false, $isAdvanced=false)
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
$element
Definition: element.phtml:12