Skip to content

Commit fb93d25

Browse files
tiagosampaiomage2pratik
authored andcommitted
Changes:
- Created a method to reorder the tabs which is less complex and works better.
1 parent 92044eb commit fb93d25

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
@@ -114,6 +114,7 @@ public function addTab($tabId, $tab)
114114
if (empty($tabId)) {
115115
throw new \Exception(__('Please correct the tab configuration and try again. Tab Id should be not empty'));
116116
}
117+
117118
if (is_array($tab)) {
118119
$this->_tabs[$tabId] = new \Magento\Framework\DataObject($tab);
119120
} elseif ($tab instanceof \Magento\Framework\DataObject) {
@@ -123,13 +124,15 @@ public function addTab($tabId, $tab)
123124
}
124125
} elseif (is_string($tab)) {
125126
$this->_addTabByName($tab, $tabId);
127+
126128
if (!$this->_tabs[$tabId] instanceof TabInterface) {
127129
unset($this->_tabs[$tabId]);
128130
return $this;
129131
}
130132
} else {
131133
throw new \Exception(__('Please correct the tab configuration and try again.'));
132134
}
135+
133136
if ($this->_tabs[$tabId]->getUrl() === null) {
134137
$this->_tabs[$tabId]->setUrl('#');
135138
}
@@ -140,10 +143,7 @@ public function addTab($tabId, $tab)
140143

141144
$this->_tabs[$tabId]->setId($tabId);
142145
$this->_tabs[$tabId]->setTabId($tabId);
143-
144-
if ($this->_activeTab === null) {
145-
$this->_activeTab = $tabId;
146-
}
146+
147147
if (true === $this->_tabs[$tabId]->getActive()) {
148148
$this->setActiveTab($tabId);
149149
}
@@ -232,32 +232,79 @@ protected function _setActiveTab($tabId)
232232
*/
233233
protected function _beforeToHtml()
234234
{
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+
235244
if ($activeTab = $this->getRequest()->getParam('active_tab')) {
236245
$this->setActiveTab($activeTab);
237246
} elseif ($activeTabId = $this->_authSession->getActiveTabId()) {
238247
$this->_setActiveTab($activeTabId);
239248
}
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+
*/
242269
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;
252284
}
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++;
253294
}
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;
260306
}
307+
261308

262309
/**
263310
* @return string

0 commit comments

Comments
 (0)