Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractComponent.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Ui\Component;
7 
16 
24 abstract class AbstractComponent extends DataObject implements UiComponentInterface
25 {
31  protected $context;
32 
36  protected $components;
37 
41  protected $componentData = [];
42 
46  protected $dataSources = [];
47 
55  public function __construct(
57  array $components = [],
58  array $data = []
59  ) {
60  $this->context = $context;
61  $this->components = $components;
62  $this->_data = array_replace_recursive($this->_data, $data);
63  $this->initObservers($this->_data);
64  }
65 
71  public function getContext()
72  {
73  return $this->context;
74  }
75 
81  public function getName()
82  {
83  return $this->getData('name');
84  }
85 
91  public function prepare()
92  {
93  $config = $this->getData('config');
94  if (isset($config['value']) && $config['value'] instanceof ValueSourceInterface) {
95  $config['value'] = $config['value']->getValue($this->getName());
96  }
97  $this->setData('config', (array)$config);
98 
99  $jsConfig = $this->getJsConfig($this);
100  if (isset($jsConfig['provider'])) {
101  unset($jsConfig['extends']);
102  $this->getContext()->addComponentDefinition($this->getName(), $jsConfig);
103  } else {
104  $this->getContext()->addComponentDefinition($this->getComponentName(), $jsConfig);
105  }
106 
107  if ($this->hasData('actions')) {
108  $this->getContext()->addActions($this->getData('actions'), $this);
109  }
110 
111  if ($this->hasData('html_blocks')) {
112  $this->getContext()->addHtmlBlocks($this->getData('html_blocks'), $this);
113  }
114 
115  if ($this->hasData('buttons')) {
116  $this->getContext()->addButtons($this->getData('buttons'), $this);
117  }
118  $this->context->getProcessor()->register($this);
119  $this->getContext()->getProcessor()->notify($this->getComponentName());
120  }
121 
129  protected function prepareChildComponent(UiComponentInterface $component)
130  {
131  $childComponents = $component->getChildComponents();
132  if (!empty($childComponents)) {
133  foreach ($childComponents as $child) {
134  $this->prepareChildComponent($child);
135  }
136  }
137  $component->prepare();
138 
139  return $this;
140  }
141 
147  public function toHtml()
148  {
149  $this->render();
150  }
151 
157  public function render()
158  {
159  $result = $this->getContext()->getRenderEngine()->render($this, $this->getTemplate());
160  return $result;
161  }
162 
170  public function addComponent($name, UiComponentInterface $component)
171  {
172  $this->components[$name] = $component;
173  }
174 
179  public function getComponent($name)
180  {
181  return isset($this->components[$name]) ? $this->components[$name] : null;
182  }
183 
189  public function getChildComponents()
190  {
191  return $this->components;
192  }
193 
200  public function renderChildComponent($name)
201  {
202  $result = null;
203  if (isset($this->components[$name])) {
204  $result = $this->components[$name]->render();
205  }
206  return $result;
207  }
208 
214  public function getTemplate()
215  {
216  return $this->getData('template') . '.xhtml';
217  }
218 
224  public function getConfiguration()
225  {
226  return (array)$this->getData('config');
227  }
228 
236  public function getJsConfig(UiComponentInterface $component)
237  {
238  $jsConfig = (array)$component->getData('js_config');
239  if (!isset($jsConfig['extends'])) {
240  $jsConfig['extends'] = $component->getContext()->getNamespace();
241  }
242  return $jsConfig;
243  }
244 
252  public function setData($key, $value = null)
253  {
254  parent::setData($key, $value);
255  }
256 
264  public function getData($key = '', $index = null)
265  {
266  return parent::getData($key, $index);
267  }
268 
275  public function prepareDataSource(array $dataSource)
276  {
277  return $dataSource;
278  }
279 
283  public function getDataSourceData()
284  {
285  return [];
286  }
287 
294  protected function initObservers(array & $data = [])
295  {
296  if (isset($data['observers']) && is_array($data['observers'])) {
297  foreach ($data['observers'] as $observerType => $observer) {
298  if (!is_object($observer)) {
299  $observer = $this;
300  }
301  if ($observer instanceof ObserverInterface) {
302  $this->getContext()->getProcessor()->attach($observerType, $observer);
303  }
304  unset($data['observers']);
305  }
306  }
307  }
308 }
getJsConfig(UiComponentInterface $component)
$config
Definition: fraud_order.php:17
prepareChildComponent(UiComponentInterface $component)
$value
Definition: gender.phtml:16
__construct(ContextInterface $context, array $components=[], array $data=[])
$index
Definition: list.phtml:44
addComponent($name, UiComponentInterface $component)
if(!isset($_GET['name'])) $name
Definition: log.php:14