-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Admin tabs order not working properly #16175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 7 commits into
magento:2.2-develop
from
tiagosampaio:hotfix/16174
Jul 13, 2018
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e0b26a7
Changes:
tiagosampaio 23d616d
Added comments and some descriptions.
tiagosampaio 5f2cf41
Tiny changes to add the type of a variable.
tiagosampaio 3252237
Better way to retrieve the first tab ID if active tab is empty.
tiagosampaio d136ac1
Sort order was split into few private methods.
tiagosampaio 633a751
Removing multiple lines.
tiagosampaio f5efeee
Removing multiple lines.
tiagosampaio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,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) { | ||
|
|
@@ -126,13 +127,15 @@ 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; | ||
| } | ||
| } else { | ||
| throw new \Exception(__('Please correct the tab configuration and try again.')); | ||
| } | ||
|
|
||
| if ($this->_tabs[$tabId]->getUrl() === null) { | ||
| $this->_tabs[$tabId]->setUrl('#'); | ||
| } | ||
|
|
@@ -143,10 +146,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); | ||
| } | ||
|
|
@@ -235,32 +235,88 @@ protected function _setActiveTab($tabId) | |
| */ | ||
| 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); | ||
| } | ||
|
|
||
| $_new = []; | ||
|
|
||
| $this->assign('tabs', $this->_tabs); | ||
| return parent::_beforeToHtml(); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| protected function reorderTabs() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to Magento backward-compatible guide we can't create new protected methods. |
||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, divide code in this function to few separate private functions. |
||
| $orderByIdentity = []; | ||
| $orderByPosition = []; | ||
|
|
||
| $position = 100; | ||
|
|
||
| /** | ||
| * Set the initial positions for each tab. | ||
| * | ||
| * @var string $key | ||
| * @var 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; | ||
|
|
||
| /** | ||
| * 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; | ||
| 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 TabInterface $tab */ | ||
| foreach ($orderByPosition as $tab) { | ||
| $ordered[$tab->getId()] = $tab; | ||
| } | ||
|
|
||
| return $ordered; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @return string | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tiagosampaio. You can select first array tab uses reset() and current() methods, please use theirs.