1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Block\Widget\Form;
/**
* Backend form container block
*
* @api
* @deprecated 100.2.0 in favour of UI component implementation
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @since 100.0.2
*/
class Container extends \Magento\Backend\Block\Widget\Container
{
/**
* @var string
*/
protected $_objectId = 'id';
/**
* @var string[]
*/
protected $_formScripts = [];
/**
* @var string[]
*/
protected $_formInitScripts = [];
/**
* @var string
*/
protected $_mode = 'edit';
/**
* @var string
*/
protected $_blockGroup = 'Magento_Backend';
/**
* @var string
*/
const PARAM_BLOCK_GROUP = 'block_group';
/**
* @var string
*/
const PARAM_MODE = 'mode';
/**
* @var string
*/
protected $_template = 'Magento_Backend::widget/form/container.phtml';
/**
* Initialize form.
*
* @return void
*/
protected function _construct()
{
parent::_construct();
if ($this->hasData(self::PARAM_BLOCK_GROUP)) {
$this->_blockGroup = $this->_getData(self::PARAM_BLOCK_GROUP);
}
if ($this->hasData(self::PARAM_MODE)) {
$this->_mode = $this->_getData(self::PARAM_MODE);
}
$this->addButton(
'back',
[
'label' => __('Back'),
'onclick' => 'setLocation(\'' . $this->getBackUrl() . '\')',
'class' => 'back'
],
-1
);
$this->addButton(
'reset',
['label' => __('Reset'), 'onclick' => 'setLocation(window.location.href)', 'class' => 'reset'],
-1
);
$objId = (int)$this->getRequest()->getParam($this->_objectId);
if (!empty($objId)) {
$this->addButton(
'delete',
[
'label' => __('Delete'),
'class' => 'delete',
'onclick' => 'deleteConfirm(\'' . __(
'Are you sure you want to do this?'
) . '\', \'' . $this->getDeleteUrl() . '\', {data: {}})'
]
);
}
$this->addButton(
'save',
[
'label' => __('Save'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save', 'target' => '#edit_form']],
]
],
1
);
}
/**
* Create form block
*
* @return $this
*/
protected function _prepareLayout()
{
if ($this->_blockGroup && $this->_controller && $this->_mode && !$this->_layout->getChildName(
$this->_nameInLayout,
'form'
)
) {
$this->addChild('form', $this->_buildFormClassName());
}
return parent::_prepareLayout();
}
/**
* Build child form class name
*
* @return string
*/
protected function _buildFormClassName()
{
return $this->nameBuilder->buildClassName(
[$this->_blockGroup, 'Block', $this->_controller, $this->_mode, 'Form']
);
}
/**
* Get URL for back (reset) button
*
* @return string
*/
public function getBackUrl()
{
return $this->getUrl('*/*/');
}
/**
* Get URL for delete button.
*
* @return string
*/
public function getDeleteUrl()
{
return $this->getUrl('*/*/delete', [$this->_objectId => (int)$this->getRequest()->getParam($this->_objectId)]);
}
/**
* Get form save URL
*
* @see getFormActionUrl()
* @return string
*/
public function getSaveUrl()
{
return $this->getFormActionUrl();
}
/**
* Get form action URL
*
* @return string
*/
public function getFormActionUrl()
{
if ($this->hasFormActionUrl()) {
return $this->getData('form_action_url');
}
return $this->getUrl('*/*/save');
}
/**
* Get form HTML.
*
* @return string
*/
public function getFormHtml()
{
$this->getChildBlock('form')->setData('action', $this->getSaveUrl());
return $this->getChildHtml('form');
}
/**
* Get form init scripts.
*
* @return string
*/
public function getFormInitScripts()
{
if (!empty($this->_formInitScripts) && is_array($this->_formInitScripts)) {
return '<script>' . implode("\n", $this->_formInitScripts) . '</script>';
}
return '';
}
/**
* Get form scripts.
*
* @return string
*/
public function getFormScripts()
{
if (!empty($this->_formScripts) && is_array($this->_formScripts)) {
return '<script>' . implode("\n", $this->_formScripts) . '</script>';
}
return '';
}
/**
* Get header width.
*
* @return string
*/
public function getHeaderWidth()
{
return '';
}
/**
* Get header css class.
*
* @return string
*/
public function getHeaderCssClass()
{
return 'icon-head head-' . strtr($this->_controller, '_', '-');
}
/**
* Get header HTML.
*
* @return string
*/
public function getHeaderHtml()
{
return '<h3 class="' . $this->getHeaderCssClass() . '">' . $this->getHeaderText() . '</h3>';
}
/**
* Set data object and pass it to form
*
* @param \Magento\Framework\DataObject $object
* @return $this
*/
public function setDataObject($object)
{
$this->getChildBlock('form')->setDataObject($object);
return $this->setData('data_object', $object);
}
}