From fb93d25e62be47bdc9270ef97fbf75f6a307c933 Mon Sep 17 00:00:00 2001 From: Tiago Sampaio Date: Fri, 15 Jun 2018 17:51:00 -0300 Subject: [PATCH 1/7] Changes: - Created a method to reorder the tabs which is less complex and works better. --- .../Magento/Backend/Block/Widget/Tabs.php | 89 ++++++++++++++----- 1 file changed, 68 insertions(+), 21 deletions(-) diff --git a/app/code/Magento/Backend/Block/Widget/Tabs.php b/app/code/Magento/Backend/Block/Widget/Tabs.php index e3479e817464a..573af01a78955 100644 --- a/app/code/Magento/Backend/Block/Widget/Tabs.php +++ b/app/code/Magento/Backend/Block/Widget/Tabs.php @@ -114,6 +114,7 @@ public function addTab($tabId, $tab) if (empty($tabId)) { throw new \Exception(__('Please correct the tab configuration and try again. Tab Id should be not empty')); } + if (is_array($tab)) { $this->_tabs[$tabId] = new \Magento\Framework\DataObject($tab); } elseif ($tab instanceof \Magento\Framework\DataObject) { @@ -123,6 +124,7 @@ public function addTab($tabId, $tab) } } elseif (is_string($tab)) { $this->_addTabByName($tab, $tabId); + if (!$this->_tabs[$tabId] instanceof TabInterface) { unset($this->_tabs[$tabId]); return $this; @@ -130,6 +132,7 @@ public function addTab($tabId, $tab) } else { throw new \Exception(__('Please correct the tab configuration and try again.')); } + if ($this->_tabs[$tabId]->getUrl() === null) { $this->_tabs[$tabId]->setUrl('#'); } @@ -140,10 +143,7 @@ public function addTab($tabId, $tab) $this->_tabs[$tabId]->setId($tabId); $this->_tabs[$tabId]->setTabId($tabId); - - if ($this->_activeTab === null) { - $this->_activeTab = $tabId; - } + if (true === $this->_tabs[$tabId]->getActive()) { $this->setActiveTab($tabId); } @@ -232,32 +232,79 @@ protected function _setActiveTab($tabId) */ protected function _beforeToHtml() { + $this->_tabs = $this->reorderTabs(); + + if ($this->_activeTab === null) { + foreach ($this->_tabs as $tab) { + $this->_activeTab = $tab->getId(); + break; + } + } + if ($activeTab = $this->getRequest()->getParam('active_tab')) { $this->setActiveTab($activeTab); } elseif ($activeTabId = $this->_authSession->getActiveTabId()) { $this->_setActiveTab($activeTabId); } - - $_new = []; + + $this->assign('tabs', $this->_tabs); + return parent::_beforeToHtml(); + } + + + /** + * @return array + */ + protected function reorderTabs() + { + $orderByIdentity = []; + $orderByPosition = []; + + $position = 100; + + /** + * @var string $key + * @var \Magento\Backend\Block\Widget\Tab\TabInterface $tab + */ foreach ($this->_tabs as $key => $tab) { - foreach ($this->_tabs as $k => $t) { - if ($t->getAfter() == $key) { - $_new[$key] = $tab; - $_new[$k] = $t; - } else { - if (!$tab->getAfter() || !in_array($tab->getAfter(), array_keys($this->_tabs))) { - $_new[$key] = $tab; - } - } + $tab->setPosition($position); + + $orderByIdentity[$key] = $tab; + $orderByPosition[$position] = $tab; + + $position += 100; + } + + $positionFactor = 1; + + foreach ($orderByPosition as $position => $tab) { + if (!$tab->getAfter() || !in_array($tab->getAfter(), array_keys($orderByIdentity))) { + $positionFactor = 1; + continue; } + + $grandPosition = $orderByIdentity[$tab->getAfter()]->getPosition(); + $newPosition = $grandPosition + $positionFactor; + + unset($orderByPosition[$position]); + $orderByPosition[$newPosition] = $tab; + $tab->setPosition($newPosition); + + $positionFactor++; } - - $this->_tabs = $_new; - unset($_new); - - $this->assign('tabs', $this->_tabs); - return parent::_beforeToHtml(); + + ksort($orderByPosition); + + $ordered = []; + + /** @var $tab */ + foreach ($orderByPosition as $tab) { + $ordered[$tab->getId()] = $tab; + } + + return $ordered; } + /** * @return string From ff940759095d965932720dadbf628fa11fc36728 Mon Sep 17 00:00:00 2001 From: Tiago Sampaio Date: Fri, 15 Jun 2018 18:05:29 -0300 Subject: [PATCH 2/7] Added comments and some descriptions. --- app/code/Magento/Backend/Block/Widget/Tabs.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Backend/Block/Widget/Tabs.php b/app/code/Magento/Backend/Block/Widget/Tabs.php index 573af01a78955..fd6c61b14dbd6 100644 --- a/app/code/Magento/Backend/Block/Widget/Tabs.php +++ b/app/code/Magento/Backend/Block/Widget/Tabs.php @@ -235,6 +235,7 @@ protected function _beforeToHtml() $this->_tabs = $this->reorderTabs(); if ($this->_activeTab === null) { + /** @var $tab */ foreach ($this->_tabs as $tab) { $this->_activeTab = $tab->getId(); break; @@ -263,8 +264,10 @@ protected function reorderTabs() $position = 100; /** - * @var string $key - * @var \Magento\Backend\Block\Widget\Tab\TabInterface $tab + * Set the initial positions for each tab. + * + * @var string $key + * @var TabInterface $tab */ foreach ($this->_tabs as $key => $tab) { $tab->setPosition($position); @@ -277,6 +280,12 @@ protected function reorderTabs() $positionFactor = 1; + /** + * Rearrange the positions by using the after tag for each tab. + * + * @var integer $position + * @var TabInterface $tab + */ foreach ($orderByPosition as $position => $tab) { if (!$tab->getAfter() || !in_array($tab->getAfter(), array_keys($orderByIdentity))) { $positionFactor = 1; From 9c7687a3d74225a5ab1f63669b48fb62e51d59cc Mon Sep 17 00:00:00 2001 From: Tiago Sampaio Date: Wed, 20 Jun 2018 12:35:07 -0300 Subject: [PATCH 3/7] Tiny changes to add the type of a variable. --- app/code/Magento/Backend/Block/Widget/Tabs.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Backend/Block/Widget/Tabs.php b/app/code/Magento/Backend/Block/Widget/Tabs.php index fd6c61b14dbd6..88508d52151b9 100644 --- a/app/code/Magento/Backend/Block/Widget/Tabs.php +++ b/app/code/Magento/Backend/Block/Widget/Tabs.php @@ -235,7 +235,7 @@ protected function _beforeToHtml() $this->_tabs = $this->reorderTabs(); if ($this->_activeTab === null) { - /** @var $tab */ + /** @var TabInterface $tab */ foreach ($this->_tabs as $tab) { $this->_activeTab = $tab->getId(); break; @@ -306,7 +306,7 @@ protected function reorderTabs() $ordered = []; - /** @var $tab */ + /** @var TabInterface $tab */ foreach ($orderByPosition as $tab) { $ordered[$tab->getId()] = $tab; } From 1aec473133d1ec2558914aafacde91e6f29ace3c Mon Sep 17 00:00:00 2001 From: Tiago Sampaio Date: Fri, 22 Jun 2018 15:45:25 -0300 Subject: [PATCH 4/7] Better way to retrieve the first tab ID if active tab is empty. --- app/code/Magento/Backend/Block/Widget/Tabs.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Backend/Block/Widget/Tabs.php b/app/code/Magento/Backend/Block/Widget/Tabs.php index 88508d52151b9..b577d80bb5db6 100644 --- a/app/code/Magento/Backend/Block/Widget/Tabs.php +++ b/app/code/Magento/Backend/Block/Widget/Tabs.php @@ -234,19 +234,16 @@ protected function _beforeToHtml() { $this->_tabs = $this->reorderTabs(); - if ($this->_activeTab === null) { - /** @var TabInterface $tab */ - foreach ($this->_tabs as $tab) { - $this->_activeTab = $tab->getId(); - break; - } - } - if ($activeTab = $this->getRequest()->getParam('active_tab')) { $this->setActiveTab($activeTab); } elseif ($activeTabId = $this->_authSession->getActiveTabId()) { $this->_setActiveTab($activeTabId); } + + if ($this->_activeTab === null && !empty($this->_tabs)) { + /** @var TabInterface $tab */ + $this->_activeTab = (reset($this->_tabs))->getId(); + } $this->assign('tabs', $this->_tabs); return parent::_beforeToHtml(); @@ -256,7 +253,7 @@ protected function _beforeToHtml() /** * @return array */ - protected function reorderTabs() + private function reorderTabs() { $orderByIdentity = []; $orderByPosition = []; From 4ac09bf0ff225fb89f64b54501d67a01b2bf6a07 Mon Sep 17 00:00:00 2001 From: Tiago Sampaio Date: Fri, 22 Jun 2018 16:12:43 -0300 Subject: [PATCH 5/7] Sort order was split into few private methods. --- .../Magento/Backend/Block/Widget/Tabs.php | 48 ++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Backend/Block/Widget/Tabs.php b/app/code/Magento/Backend/Block/Widget/Tabs.php index b577d80bb5db6..cde86ef243e87 100644 --- a/app/code/Magento/Backend/Block/Widget/Tabs.php +++ b/app/code/Magento/Backend/Block/Widget/Tabs.php @@ -251,14 +251,15 @@ protected function _beforeToHtml() /** + * Reorder the tabs. + * * @return array */ private function reorderTabs() { $orderByIdentity = []; $orderByPosition = []; - - $position = 100; + $position = 100; /** * Set the initial positions for each tab. @@ -274,9 +275,21 @@ private function reorderTabs() $position += 100; } - + + return $this->applyTabsCorrectOrder($orderByPosition, $orderByIdentity); + } + + + /** + * @param array $orderByPosition + * @param array $orderByIdentity + * + * @return array + */ + private function applyTabsCorrectOrder(array $orderByPosition, array $orderByIdentity) + { $positionFactor = 1; - + /** * Rearrange the positions by using the after tag for each tab. * @@ -288,26 +301,39 @@ private function reorderTabs() $positionFactor = 1; continue; } - + $grandPosition = $orderByIdentity[$tab->getAfter()]->getPosition(); $newPosition = $grandPosition + $positionFactor; - + unset($orderByPosition[$position]); $orderByPosition[$newPosition] = $tab; $tab->setPosition($newPosition); - + $positionFactor++; } - + + return $this->finalTabsSortOrder($orderByPosition); + } + + + /** + * Apply the last sort order to tabs. + * + * @param array $orderByPosition + * + * @return array + */ + private function finalTabsSortOrder(array $orderByPosition) + { ksort($orderByPosition); - + $ordered = []; - + /** @var TabInterface $tab */ foreach ($orderByPosition as $tab) { $ordered[$tab->getId()] = $tab; } - + return $ordered; } From 787ab8bd602b562940f59fe88856763bb07192c5 Mon Sep 17 00:00:00 2001 From: Tiago Sampaio Date: Tue, 26 Jun 2018 20:00:48 -0300 Subject: [PATCH 6/7] Removing multiple lines. --- app/code/Magento/Backend/Block/Widget/Tabs.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/code/Magento/Backend/Block/Widget/Tabs.php b/app/code/Magento/Backend/Block/Widget/Tabs.php index cde86ef243e87..17a276b8d588c 100644 --- a/app/code/Magento/Backend/Block/Widget/Tabs.php +++ b/app/code/Magento/Backend/Block/Widget/Tabs.php @@ -249,7 +249,6 @@ protected function _beforeToHtml() return parent::_beforeToHtml(); } - /** * Reorder the tabs. * @@ -337,7 +336,6 @@ private function finalTabsSortOrder(array $orderByPosition) return $ordered; } - /** * @return string */ From 6ca22c56753aaee394aa7f6f2a52344711dd0dd1 Mon Sep 17 00:00:00 2001 From: Tiago Sampaio Date: Wed, 27 Jun 2018 11:42:28 -0300 Subject: [PATCH 7/7] Removing multiple lines. --- app/code/Magento/Backend/Block/Widget/Tabs.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/code/Magento/Backend/Block/Widget/Tabs.php b/app/code/Magento/Backend/Block/Widget/Tabs.php index 17a276b8d588c..3730b41f9b3a5 100644 --- a/app/code/Magento/Backend/Block/Widget/Tabs.php +++ b/app/code/Magento/Backend/Block/Widget/Tabs.php @@ -277,8 +277,7 @@ private function reorderTabs() return $this->applyTabsCorrectOrder($orderByPosition, $orderByIdentity); } - - + /** * @param array $orderByPosition * @param array $orderByIdentity @@ -314,7 +313,6 @@ private function applyTabsCorrectOrder(array $orderByPosition, array $orderByIde return $this->finalTabsSortOrder($orderByPosition); } - /** * Apply the last sort order to tabs. *