Skip to content
Merged
Changes from 3 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
98 changes: 77 additions & 21 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,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) {

Copy link
Copy Markdown
Contributor

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.

$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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
Expand Down