@@ -117,6 +117,7 @@ public function addTab($tabId, $tab)
117
117
if (empty ($ tabId )) {
118
118
throw new \Exception (__ ('Please correct the tab configuration and try again. Tab Id should be not empty ' ));
119
119
}
120
+
120
121
if (is_array ($ tab )) {
121
122
$ this ->_tabs [$ tabId ] = new \Magento \Framework \DataObject ($ tab );
122
123
} elseif ($ tab instanceof \Magento \Framework \DataObject) {
@@ -126,13 +127,15 @@ public function addTab($tabId, $tab)
126
127
}
127
128
} elseif (is_string ($ tab )) {
128
129
$ this ->_addTabByName ($ tab , $ tabId );
130
+
129
131
if (!$ this ->_tabs [$ tabId ] instanceof TabInterface) {
130
132
unset($ this ->_tabs [$ tabId ]);
131
133
return $ this ;
132
134
}
133
135
} else {
134
136
throw new \Exception (__ ('Please correct the tab configuration and try again. ' ));
135
137
}
138
+
136
139
if ($ this ->_tabs [$ tabId ]->getUrl () === null ) {
137
140
$ this ->_tabs [$ tabId ]->setUrl ('# ' );
138
141
}
@@ -143,10 +146,7 @@ public function addTab($tabId, $tab)
143
146
144
147
$ this ->_tabs [$ tabId ]->setId ($ tabId );
145
148
$ this ->_tabs [$ tabId ]->setTabId ($ tabId );
146
-
147
- if ($ this ->_activeTab === null ) {
148
- $ this ->_activeTab = $ tabId ;
149
- }
149
+
150
150
if (true === $ this ->_tabs [$ tabId ]->getActive ()) {
151
151
$ this ->setActiveTab ($ tabId );
152
152
}
@@ -235,33 +235,108 @@ protected function _setActiveTab($tabId)
235
235
*/
236
236
protected function _beforeToHtml ()
237
237
{
238
+ $ this ->_tabs = $ this ->reorderTabs ();
239
+
238
240
if ($ activeTab = $ this ->getRequest ()->getParam ('active_tab ' )) {
239
241
$ this ->setActiveTab ($ activeTab );
240
242
} elseif ($ activeTabId = $ this ->_authSession ->getActiveTabId ()) {
241
243
$ this ->_setActiveTab ($ activeTabId );
242
244
}
243
245
244
- $ _new = [];
246
+ if ($ this ->_activeTab === null && !empty ($ this ->_tabs )) {
247
+ /** @var TabInterface $tab */
248
+ $ this ->_activeTab = (reset ($ this ->_tabs ))->getId ();
249
+ }
250
+
251
+ $ this ->assign ('tabs ' , $ this ->_tabs );
252
+ return parent ::_beforeToHtml ();
253
+ }
254
+
255
+ /**
256
+ * Reorder the tabs.
257
+ *
258
+ * @return array
259
+ */
260
+ private function reorderTabs ()
261
+ {
262
+ $ orderByIdentity = [];
263
+ $ orderByPosition = [];
264
+ $ position = 100 ;
265
+
266
+ /**
267
+ * Set the initial positions for each tab.
268
+ *
269
+ * @var string $key
270
+ * @var TabInterface $tab
271
+ */
245
272
foreach ($ this ->_tabs as $ key => $ tab ) {
246
- foreach ($ this ->_tabs as $ k => $ t ) {
247
- if ($ t ->getAfter () == $ key ) {
248
- $ _new [$ key ] = $ tab ;
249
- $ _new [$ k ] = $ t ;
250
- } else {
251
- if (!$ tab ->getAfter () || !in_array ($ tab ->getAfter (), array_keys ($ this ->_tabs ))) {
252
- $ _new [$ key ] = $ tab ;
253
- }
254
- }
255
- }
273
+ $ tab ->setPosition ($ position );
274
+
275
+ $ orderByIdentity [$ key ] = $ tab ;
276
+ $ orderByPosition [$ position ] = $ tab ;
277
+
278
+ $ position += 100 ;
256
279
}
257
280
258
- $ this ->_tabs = $ _new ;
259
- unset( $ _new );
281
+ return $ this ->applyTabsCorrectOrder ( $ orderByPosition , $ orderByIdentity ) ;
282
+ }
260
283
261
- $ this ->assign ('tabs ' , $ this ->_tabs );
262
- return parent ::_beforeToHtml ();
284
+ /**
285
+ * @param array $orderByPosition
286
+ * @param array $orderByIdentity
287
+ *
288
+ * @return array
289
+ */
290
+ private function applyTabsCorrectOrder (array $ orderByPosition , array $ orderByIdentity )
291
+ {
292
+ $ positionFactor = 1 ;
293
+
294
+ /**
295
+ * Rearrange the positions by using the after tag for each tab.
296
+ *
297
+ * @var integer $position
298
+ * @var TabInterface $tab
299
+ */
300
+ foreach ($ orderByPosition as $ position => $ tab ) {
301
+ if (!$ tab ->getAfter () || !in_array ($ tab ->getAfter (), array_keys ($ orderByIdentity ))) {
302
+ $ positionFactor = 1 ;
303
+ continue ;
304
+ }
305
+
306
+ $ grandPosition = $ orderByIdentity [$ tab ->getAfter ()]->getPosition ();
307
+ $ newPosition = $ grandPosition + $ positionFactor ;
308
+
309
+ unset($ orderByPosition [$ position ]);
310
+ $ orderByPosition [$ newPosition ] = $ tab ;
311
+ $ tab ->setPosition ($ newPosition );
312
+
313
+ $ positionFactor ++;
314
+ }
315
+
316
+ return $ this ->finalTabsSortOrder ($ orderByPosition );
263
317
}
264
318
319
+ /**
320
+ * Apply the last sort order to tabs.
321
+ *
322
+ * @param array $orderByPosition
323
+ *
324
+ * @return array
325
+ */
326
+ private function finalTabsSortOrder (array $ orderByPosition )
327
+ {
328
+ ksort ($ orderByPosition );
329
+
330
+ $ ordered = [];
331
+
332
+ /** @var TabInterface $tab */
333
+ foreach ($ orderByPosition as $ tab ) {
334
+ $ ordered [$ tab ->getId ()] = $ tab ;
335
+ }
336
+
337
+ return $ ordered ;
338
+ }
339
+
265
340
/**
266
341
* @return string
267
342
*/
0 commit comments