66namespace Magento \Ui \Component ;
77
88use Magento \Framework \DataObject ;
9+ use Magento \Framework \Exception \LocalizedException ;
10+ use Magento \Framework \View \Element \UiComponentFactory ;
911use Magento \Framework \View \Element \UiComponentInterface ;
1012use Magento \Framework \View \Element \UiComponent \ContextInterface ;
1113use Magento \Framework \View \Element \UiComponent \DataSourceInterface ;
@@ -85,19 +87,92 @@ public function getName()
8587 */
8688 public function prepare ()
8789 {
90+ if ($ this ->getData (UiComponentFactory::CHILDREN_FROM_META )) {
91+ $ children = (array )$ this ->getContext ()->getDataProvider ()->getMeta ();
92+ foreach ($ children as $ name => $ childData ) {
93+ $ this ->createChildComponent ($ name , $ childData );
94+ }
95+ }
96+
8897 $ jsConfig = $ this ->getJsConfig ($ this );
8998 if (isset ($ jsConfig ['provider ' ])) {
9099 unset($ jsConfig ['extends ' ]);
91100 $ this ->getContext ()->addComponentDefinition ($ this ->getName (), $ jsConfig );
92101 } else {
93102 $ this ->getContext ()->addComponentDefinition ($ this ->getComponentName (), $ jsConfig );
94103 }
104+
105+ if ($ this ->hasData ('actions ' )) {
106+ $ this ->getContext ()->addActions ($ this ->getData ('actions ' ), $ this );
107+ }
108+
95109 if ($ this ->hasData ('buttons ' )) {
96110 $ this ->getContext ()->addButtons ($ this ->getData ('buttons ' ), $ this );
97111 }
98112 $ this ->getContext ()->getProcessor ()->notify ($ this ->getComponentName ());
99113 }
100114
115+ /**
116+ * Create child Ui Component
117+ *
118+ * @param string $name
119+ * @param array $childData
120+ * @return $this
121+ * @throws \Magento\Framework\Exception\LocalizedException
122+ */
123+ protected function createChildComponent ($ name , array $ childData )
124+ {
125+ if (empty ($ childData )) {
126+ return $ this ;
127+ }
128+
129+ $ childComponent = $ this ->getComponent ($ name );
130+ if ($ childComponent === null ) {
131+ $ argument = [
132+ 'context ' => $ this ->getContext (),
133+ 'data ' => [
134+ 'name ' => $ name ,
135+ 'config ' => $ childData
136+ ]
137+ ];
138+
139+ if (!isset ($ childData ['componentType ' ])) {
140+ throw new LocalizedException (
141+ __ ('The configuration parameter "componentType" is a required for "%1" component. ' , $ name )
142+ );
143+ }
144+
145+ $ childComponent = $ this ->getContext ()
146+ ->getUiComponentFactory ()
147+ ->create ($ name , $ childData ['componentType ' ], $ argument );
148+ $ this ->prepareChildComponent ($ childComponent );
149+ $ this ->addComponent ($ name , $ childComponent );
150+ } else {
151+ $ this ->updateComponent ($ childData , $ childComponent );
152+ }
153+
154+ return $ this ;
155+ }
156+
157+ /**
158+ * Call prepare method in the component UI
159+ *
160+ * @param UiComponentInterface $component
161+ * @return $this
162+ */
163+ protected function prepareChildComponent (UiComponentInterface $ component )
164+ {
165+ $ childComponents = $ component ->getChildComponents ();
166+ if (!empty ($ childComponents )) {
167+ foreach ($ childComponents as $ child ) {
168+ $ this ->prepareChildComponent ($ child );
169+ }
170+ }
171+ $ component ->prepare ();
172+
173+ return $ this ;
174+ }
175+
101176 /**
102177 * Produce and return block's html output
103178 *
@@ -182,7 +257,7 @@ public function getTemplate()
182257 */
183258 public function getConfiguration ()
184259 {
185- return (array ) $ this ->getData ('config ' );
260+ return (array )$ this ->getData ('config ' );
186261 }
187262
188263 /**
@@ -194,7 +269,7 @@ public function getConfiguration()
194269 */
195270 public function getJsConfig (UiComponentInterface $ component )
196271 {
197- $ jsConfig = (array ) $ component ->getData ('js_config ' );
272+ $ jsConfig = (array )$ component ->getData ('js_config ' );
198273 if (!isset ($ jsConfig ['extends ' ])) {
199274 $ jsConfig ['extends ' ] = $ component ->getContext ()->getNamespace ();
200275 }
@@ -264,4 +339,36 @@ protected function initObservers(array & $data = [])
264339 }
265340 }
266341 }
342+
343+ /**
344+ * Update component data
345+ *
346+ * @param array $componentData
347+ * @param UiComponentInterface $component
348+ * @return $this
349+ */
350+ protected function updateComponent (array $ componentData , UiComponentInterface $ component )
351+ {
352+ $ config = $ component ->getData ('config ' );
353+ // XML data configuration override configuration coming from the DB
354+ $ config = array_replace_recursive ($ componentData , $ config );
355+ $ component ->setData ('config ' , $ config );
356+
357+ return $ this ;
358+ }
359+
360+ /**
361+ * Update DataScope
362+ *
363+ * @param array $data
364+ * @param string $name
365+ * @return array
366+ */
367+ protected function updateDataScope (array $ data , $ name )
368+ {
369+ if (!isset ($ data ['dataScope ' ])) {
370+ $ data ['dataScope ' ] = $ name ;
371+ }
372+ return $ data ;
373+ }
267374}
0 commit comments