Skip to content

Admin tabs order not working properly #16412

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
merged 9 commits into from
Jul 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 94 additions & 19 deletions app/code/Magento/Backend/Block/Widget/Tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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('#');
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -235,33 +235,108 @@ protected function _setActiveTab($tabId)
*/
protected function _beforeToHtml()
{
$this->_tabs = $this->reorderTabs();

if ($activeTab = $this->getRequest()->getParam('active_tab')) {
$this->setActiveTab($activeTab);
} elseif ($activeTabId = $this->_authSession->getActiveTabId()) {
$this->_setActiveTab($activeTabId);
}

$_new = [];
if ($this->_activeTab === null && !empty($this->_tabs)) {
/** @var TabInterface $tab */
$this->_activeTab = (reset($this->_tabs))->getId();
}

$this->assign('tabs', $this->_tabs);
return parent::_beforeToHtml();
}

/**
* Reorder the tabs.
*
* @return array
*/
private function reorderTabs()
{
$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;
}

$this->_tabs = $_new;
unset($_new);
return $this->applyTabsCorrectOrder($orderByPosition, $orderByIdentity);
}

$this->assign('tabs', $this->_tabs);
return parent::_beforeToHtml();
/**
* @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.
*
* @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++;
}

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;
}

/**
* @return string
*/
Expand Down