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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Ui\Component;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponent\DataSourceInterface;
use Magento\Framework\View\Element\UiComponent\ObserverInterface;
use Magento\Framework\Data\ValueSourceInterface;
/**
* Abstract class AbstractComponent
*
* @api
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @since 100.0.2
*/
abstract class AbstractComponent extends DataObject implements UiComponentInterface
{
/**
* Render context
*
* @var ContextInterface
*/
protected $context;
/**
* @var UiComponentInterface[]
*/
protected $components;
/**
* @var array
*/
protected $componentData = [];
/**
* @var DataSourceInterface[]
*/
protected $dataSources = [];
/**
* Constructor
*
* @param ContextInterface $context
* @param UiComponentInterface[] $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
array $components = [],
array $data = []
) {
$this->context = $context;
$this->components = $components;
$this->_data = array_replace_recursive($this->_data, $data);
$this->initObservers($this->_data);
}
/**
* Get component context
*
* @return ContextInterface
*/
public function getContext()
{
return $this->context;
}
/**
* Get component name
*
* @return string
*/
public function getName()
{
return $this->getData('name');
}
/**
* Prepare component configuration
*
* @return void
*/
public function prepare()
{
$config = $this->getData('config');
if (isset($config['value']) && $config['value'] instanceof ValueSourceInterface) {
$config['value'] = $config['value']->getValue($this->getName());
}
$this->setData('config', (array)$config);
$jsConfig = $this->getJsConfig($this);
if (isset($jsConfig['provider'])) {
unset($jsConfig['extends']);
$this->getContext()->addComponentDefinition($this->getName(), $jsConfig);
} else {
$this->getContext()->addComponentDefinition($this->getComponentName(), $jsConfig);
}
if ($this->hasData('actions')) {
$this->getContext()->addActions($this->getData('actions'), $this);
}
if ($this->hasData('html_blocks')) {
$this->getContext()->addHtmlBlocks($this->getData('html_blocks'), $this);
}
if ($this->hasData('buttons')) {
$this->getContext()->addButtons($this->getData('buttons'), $this);
}
$this->context->getProcessor()->register($this);
$this->getContext()->getProcessor()->notify($this->getComponentName());
}
/**
* Call prepare method in the component UI
*
* @param UiComponentInterface $component
* @return $this
* @since 100.1.0
*/
protected function prepareChildComponent(UiComponentInterface $component)
{
$childComponents = $component->getChildComponents();
if (!empty($childComponents)) {
foreach ($childComponents as $child) {
$this->prepareChildComponent($child);
}
}
$component->prepare();
return $this;
}
/**
* Produce and return block's html output
*
* @return string
*/
public function toHtml()
{
$this->render();
}
/**
* Render component
*
* @return string
*/
public function render()
{
$result = $this->getContext()->getRenderEngine()->render($this, $this->getTemplate());
return $result;
}
/**
* Add component
*
* @param string $name
* @param UiComponentInterface $component
* @return void
*/
public function addComponent($name, UiComponentInterface $component)
{
$this->components[$name] = $component;
}
/**
* @param string $name
* @return UiComponentInterface
*/
public function getComponent($name)
{
return isset($this->components[$name]) ? $this->components[$name] : null;
}
/**
* Get components
*
* @return UiComponentInterface[]
*/
public function getChildComponents()
{
return $this->components;
}
/**
* Render child component
*
* @param string $name
* @return string
*/
public function renderChildComponent($name)
{
$result = null;
if (isset($this->components[$name])) {
$result = $this->components[$name]->render();
}
return $result;
}
/**
* Get template
*
* @return string
*/
public function getTemplate()
{
return $this->getData('template') . '.xhtml';
}
/**
* Get component configuration
*
* @return array
*/
public function getConfiguration()
{
return (array)$this->getData('config');
}
/**
* Get configuration of related JavaScript Component
* (force extending the root component if component does not extend other component)
*
* @param UiComponentInterface $component
* @return array
*/
public function getJsConfig(UiComponentInterface $component)
{
$jsConfig = (array)$component->getData('js_config');
if (!isset($jsConfig['extends'])) {
$jsConfig['extends'] = $component->getContext()->getNamespace();
}
return $jsConfig;
}
/**
* Component data setter
*
* @param string|array $key
* @param mixed $value
* @return void
*/
public function setData($key, $value = null)
{
parent::setData($key, $value);
}
/**
* Component data getter
*
* @param string $key
* @param string|int $index
* @return mixed
*/
public function getData($key = '', $index = null)
{
return parent::getData($key, $index);
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
return $dataSource;
}
/**
* {@inheritdoc}
*/
public function getDataSourceData()
{
return [];
}
/**
* Initiate observers
*
* @param array $data
* @return void
*/
protected function initObservers(array & $data = [])
{
if (isset($data['observers']) && is_array($data['observers'])) {
foreach ($data['observers'] as $observerType => $observer) {
if (!is_object($observer)) {
$observer = $this;
}
if ($observer instanceof ObserverInterface) {
$this->getContext()->getProcessor()->attach($observerType, $observer);
}
unset($data['observers']);
}
}
}
}