@@ -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,32 +232,79 @@ protected function _setActiveTab($tabId)
232
232
*/
233
233
protected function _beforeToHtml ()
234
234
{
235
+ $ this ->_tabs = $ this ->reorderTabs ();
236
+
237
+ if ($ this ->_activeTab === null ) {
238
+ foreach ($ this ->_tabs as $ tab ) {
239
+ $ this ->_activeTab = $ tab ->getId ();
240
+ break ;
241
+ }
242
+ }
243
+
235
244
if ($ activeTab = $ this ->getRequest ()->getParam ('active_tab ' )) {
236
245
$ this ->setActiveTab ($ activeTab );
237
246
} elseif ($ activeTabId = $ this ->_authSession ->getActiveTabId ()) {
238
247
$ this ->_setActiveTab ($ activeTabId );
239
248
}
240
-
241
- $ _new = [];
249
+
250
+ $ this ->assign ('tabs ' , $ this ->_tabs );
251
+ return parent ::_beforeToHtml ();
252
+ }
253
+
254
+
255
+ /**
256
+ * @return array
257
+ */
258
+ protected function reorderTabs ()
259
+ {
260
+ $ orderByIdentity = [];
261
+ $ orderByPosition = [];
262
+
263
+ $ position = 100 ;
264
+
265
+ /**
266
+ * @var string $key
267
+ * @var \Magento\Backend\Block\Widget\Tab\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
- }
270
+ $ tab ->setPosition ($ position );
271
+
272
+ $ orderByIdentity [$ key ] = $ tab ;
273
+ $ orderByPosition [$ position ] = $ tab ;
274
+
275
+ $ position += 100 ;
276
+ }
277
+
278
+ $ positionFactor = 1 ;
279
+
280
+ foreach ($ orderByPosition as $ position => $ tab ) {
281
+ if (!$ tab ->getAfter () || !in_array ($ tab ->getAfter (), array_keys ($ orderByIdentity ))) {
282
+ $ positionFactor = 1 ;
283
+ continue ;
252
284
}
285
+
286
+ $ grandPosition = $ orderByIdentity [$ tab ->getAfter ()]->getPosition ();
287
+ $ newPosition = $ grandPosition + $ positionFactor ;
288
+
289
+ unset($ orderByPosition [$ position ]);
290
+ $ orderByPosition [$ newPosition ] = $ tab ;
291
+ $ tab ->setPosition ($ newPosition );
292
+
293
+ $ positionFactor ++;
253
294
}
254
-
255
- $ this ->_tabs = $ _new ;
256
- unset($ _new );
257
-
258
- $ this ->assign ('tabs ' , $ this ->_tabs );
259
- return parent ::_beforeToHtml ();
295
+
296
+ ksort ($ orderByPosition );
297
+
298
+ $ ordered = [];
299
+
300
+ /** @var $tab */
301
+ foreach ($ orderByPosition as $ tab ) {
302
+ $ ordered [$ tab ->getId ()] = $ tab ;
303
+ }
304
+
305
+ return $ ordered ;
260
306
}
307
+
261
308
262
309
/**
263
310
* @return string
0 commit comments