Skip to content

Commit e0b26a7

Browse files
committed
Changes:
- Created a method to reorder the tabs which is less complex and works better.
1 parent 61c3ba6 commit e0b26a7

File tree

1 file changed

+68
-21
lines changed
  • app/code/Magento/Backend/Block/Widget

1 file changed

+68
-21
lines changed

app/code/Magento/Backend/Block/Widget/Tabs.php

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function addTab($tabId, $tab)
117117
if (empty($tabId)) {
118118
throw new \Exception(__('Please correct the tab configuration and try again. Tab Id should be not empty'));
119119
}
120+
120121
if (is_array($tab)) {
121122
$this->_tabs[$tabId] = new \Magento\Framework\DataObject($tab);
122123
} elseif ($tab instanceof \Magento\Framework\DataObject) {
@@ -126,13 +127,15 @@ public function addTab($tabId, $tab)
126127
}
127128
} elseif (is_string($tab)) {
128129
$this->_addTabByName($tab, $tabId);
130+
129131
if (!$this->_tabs[$tabId] instanceof TabInterface) {
130132
unset($this->_tabs[$tabId]);
131133
return $this;
132134
}
133135
} else {
134136
throw new \Exception(__('Please correct the tab configuration and try again.'));
135137
}
138+
136139
if ($this->_tabs[$tabId]->getUrl() === null) {
137140
$this->_tabs[$tabId]->setUrl('#');
138141
}
@@ -143,10 +146,7 @@ public function addTab($tabId, $tab)
143146

144147
$this->_tabs[$tabId]->setId($tabId);
145148
$this->_tabs[$tabId]->setTabId($tabId);
146-
147-
if ($this->_activeTab === null) {
148-
$this->_activeTab = $tabId;
149-
}
149+
150150
if (true === $this->_tabs[$tabId]->getActive()) {
151151
$this->setActiveTab($tabId);
152152
}
@@ -235,32 +235,79 @@ protected function _setActiveTab($tabId)
235235
*/
236236
protected function _beforeToHtml()
237237
{
238+
$this->_tabs = $this->reorderTabs();
239+
240+
if ($this->_activeTab === null) {
241+
foreach ($this->_tabs as $tab) {
242+
$this->_activeTab = $tab->getId();
243+
break;
244+
}
245+
}
246+
238247
if ($activeTab = $this->getRequest()->getParam('active_tab')) {
239248
$this->setActiveTab($activeTab);
240249
} elseif ($activeTabId = $this->_authSession->getActiveTabId()) {
241250
$this->_setActiveTab($activeTabId);
242251
}
243-
244-
$_new = [];
252+
253+
$this->assign('tabs', $this->_tabs);
254+
return parent::_beforeToHtml();
255+
}
256+
257+
258+
/**
259+
* @return array
260+
*/
261+
protected function reorderTabs()
262+
{
263+
$orderByIdentity = [];
264+
$orderByPosition = [];
265+
266+
$position = 100;
267+
268+
/**
269+
* @var string $key
270+
* @var \Magento\Backend\Block\Widget\Tab\TabInterface $tab
271+
*/
245272
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-
}
273+
$tab->setPosition($position);
274+
275+
$orderByIdentity[$key] = $tab;
276+
$orderByPosition[$position] = $tab;
277+
278+
$position += 100;
279+
}
280+
281+
$positionFactor = 1;
282+
283+
foreach ($orderByPosition as $position => $tab) {
284+
if (!$tab->getAfter() || !in_array($tab->getAfter(), array_keys($orderByIdentity))) {
285+
$positionFactor = 1;
286+
continue;
255287
}
288+
289+
$grandPosition = $orderByIdentity[$tab->getAfter()]->getPosition();
290+
$newPosition = $grandPosition + $positionFactor;
291+
292+
unset($orderByPosition[$position]);
293+
$orderByPosition[$newPosition] = $tab;
294+
$tab->setPosition($newPosition);
295+
296+
$positionFactor++;
256297
}
257-
258-
$this->_tabs = $_new;
259-
unset($_new);
260-
261-
$this->assign('tabs', $this->_tabs);
262-
return parent::_beforeToHtml();
298+
299+
ksort($orderByPosition);
300+
301+
$ordered = [];
302+
303+
/** @var $tab */
304+
foreach ($orderByPosition as $tab) {
305+
$ordered[$tab->getId()] = $tab;
306+
}
307+
308+
return $ordered;
263309
}
310+
264311

265312
/**
266313
* @return string

0 commit comments

Comments
 (0)