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