41 protected $_template =
'Magento_Backend::widget/tabs.phtml';
51 private $_jsonEncoder;
61 \
Magento\Framework\Json\EncoderInterface $jsonEncoder,
62 \
Magento\Backend\Model\Auth\Session $authSession,
65 $this->_authSession = $authSession;
66 parent::__construct($context,
$data);
67 $this->_jsonEncoder = $jsonEncoder;
88 $this->_destElementId = $elementId;
102 $this->
addTab($tabId, $tab);
103 $this->_tabs[$tabId]->setAfter($afterTabId);
118 throw new \Exception(
__(
'Please correct the tab configuration and try again. Tab Id should be not empty'));
121 if (is_array($tab)) {
122 $this->_tabs[$tabId] = new \Magento\Framework\DataObject($tab);
124 $this->_tabs[$tabId] = $tab;
125 if (!$this->_tabs[$tabId]->hasTabId()) {
126 $this->_tabs[$tabId]->setTabId($tabId);
128 }
elseif (is_string($tab)) {
132 unset($this->_tabs[$tabId]);
136 throw new \Exception(
__(
'Please correct the tab configuration and try again.'));
139 if ($this->_tabs[$tabId]->
getUrl() ===
null) {
140 $this->_tabs[$tabId]->setUrl(
'#');
143 if (!$this->_tabs[$tabId]->getTitle()) {
144 $this->_tabs[$tabId]->setTitle($this->_tabs[$tabId]->getLabel());
147 $this->_tabs[$tabId]->setId($tabId);
148 $this->_tabs[$tabId]->setTabId($tabId);
150 if (
true === $this->_tabs[$tabId]->getActive()) {
167 if (strpos($tab,
'\Block\\') !==
false) {
172 $this->_tabs[$tabId] =
null;
175 if ($this->_tabs[$tabId] !==
null && !$this->_tabs[$tabId] instanceof
TabInterface) {
176 throw new \Exception(
__(
'Please correct the tab configuration and try again.'));
185 return $this->
getTabId($this->_tabs[$this->_activeTab]);
205 $this->_activeTab = $tabId;
206 if ($this->_activeTab !==
null && $tabId !== $this->_activeTab) {
207 foreach ($this->_tabs as
$id => $tab) {
208 $tab->setActive(
$id === $tabId);
223 foreach ($this->_tabs as
$id => $tab) {
224 if ($this->
getTabId($tab) == $tabId) {
225 $this->_activeTab =
$id;
226 $tab->setActive(
true);
238 $this->_tabs = $this->reorderTabs();
240 if ($activeTab = $this->
getRequest()->getParam(
'active_tab')) {
242 }
elseif ($activeTabId = $this->_authSession->getActiveTabId()) {
246 if ($this->_activeTab ===
null && !empty($this->_tabs)) {
248 $this->_activeTab = (reset($this->_tabs))->
getId();
251 $this->
assign(
'tabs', $this->_tabs);
252 return parent::_beforeToHtml();
260 private function reorderTabs()
262 $orderByIdentity = [];
263 $orderByPosition = [];
272 foreach ($this->_tabs as $key => $tab) {
273 $tab->setPosition($position);
275 $orderByIdentity[$key] = $tab;
276 $orderByPosition[$position] = $tab;
281 return $this->applyTabsCorrectOrder($orderByPosition, $orderByIdentity);
290 private function applyTabsCorrectOrder(array $orderByPosition, array $orderByIdentity)
300 foreach ($orderByPosition as $position => $tab) {
301 if (!$tab->getAfter() || !in_array($tab->getAfter(), array_keys($orderByIdentity))) {
306 $grandPosition = $orderByIdentity[$tab->getAfter()]->getPosition();
307 $newPosition = $grandPosition + $positionFactor;
309 unset($orderByPosition[$position]);
310 $orderByPosition[$newPosition] = $tab;
311 $tab->setPosition($newPosition);
316 return $this->finalTabsSortOrder($orderByPosition);
326 private function finalTabsSortOrder(array $orderByPosition)
328 ksort($orderByPosition);
333 foreach ($orderByPosition as $tab) {
334 $ordered[$tab->getId()] = $tab;
345 return $this->
getId() .
'JsTabs';
353 if (empty($this->_tabs)) {
357 return array_keys($this->_tabs);
368 return ($withPrefix ? $this->
getId() .
'_' :
'') . $tab->getTabId();
370 return ($withPrefix ? $this->
getId() .
'_' :
'') . $tab->getId();
380 return $tab->canShowTab();
393 return $tab->isHidden();
395 return $tab->getIsHidden();
405 if (method_exists($tab,
'getTabUrl')) {
406 return $tab->getTabUrl();
410 if ($tab->getUrl() !==
null) {
411 return $tab->getUrl();
423 return $tab->getTabTitle();
425 return $tab->getTitle();
435 if (method_exists($tab,
'getTabClass')) {
436 return $tab->getTabClass();
440 return $tab->getClass();
450 return $tab->getTabLabel();
452 return $tab->getLabel();
462 if ($tab->getSkipGenerateContent()) {
465 return $tab->toHtml();
467 return $tab->getContent();
482 $args = func_get_args();
483 if (!empty($args) && count($args) > 1) {
484 foreach ($args as $tabId) {
485 if (isset($this->_tabs[$tabId])) {
486 $tabs[$tabId] = $tabId;
489 $blockId = $this->
getId();
490 foreach ($tabs as $tabId) {
491 foreach ($tabs as $tabToId) {
492 if ($tabId !== $tabToId) {
493 if (!$this->_tabs[$tabToId]->
getData(
'shadow_tabs')) {
494 $this->_tabs[$tabToId]->setData(
'shadow_tabs', []);
496 $this->_tabs[$tabToId]->setData(
498 array_merge($this->_tabs[$tabToId]->
getData(
'shadow_tabs'), [$blockId .
'_' . $tabId])
515 if (!empty($this->_tabs)) {
516 $blockId = $this->
getId();
517 foreach (array_keys($this->_tabs) as $tabId) {
518 if ($this->_tabs[$tabId]->
getData(
'shadow_tabs')) {
519 $result[$blockId .
'_' . $tabId] = $this->_tabs[$tabId]->getData(
'shadow_tabs');
524 return $this->_jsonEncoder->encode(
$result);
539 if (isset($this->_tabs[$tab]) && $this->_tabs[$tab] instanceof \
Magento\Framework\DataObject) {
543 $this->_tabs[$tab]->setData($key,
$value);
557 if (isset($this->_tabs[$tabId])) {
558 unset($this->_tabs[$tabId]);
getData($key='', $index=null)
elseif(isset( $params[ 'redirect_parent']))
getUrl($route='', $params=[])
assign($key, $value=null)