Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tabs.php
Go to the documentation of this file.
1 <?php
7 
9 
16 {
22  protected $_tabs = [];
23 
29  protected $_activeTab = null;
30 
36  protected $_destElementId = 'content';
37 
41  protected $_template = 'Magento_Backend::widget/tabs.phtml';
42 
46  protected $_authSession;
47 
51  private $_jsonEncoder;
52 
59  public function __construct(
60  \Magento\Backend\Block\Template\Context $context,
61  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
62  \Magento\Backend\Model\Auth\Session $authSession,
63  array $data = []
64  ) {
65  $this->_authSession = $authSession;
66  parent::__construct($context, $data);
67  $this->_jsonEncoder = $jsonEncoder;
68  }
69 
75  public function getDestElementId()
76  {
77  return $this->_destElementId;
78  }
79 
86  public function setDestElementId($elementId)
87  {
88  $this->_destElementId = $elementId;
89  return $this;
90  }
91 
100  public function addTabAfter($tabId, $tab, $afterTabId)
101  {
102  $this->addTab($tabId, $tab);
103  $this->_tabs[$tabId]->setAfter($afterTabId);
104  }
105 
115  public function addTab($tabId, $tab)
116  {
117  if (empty($tabId)) {
118  throw new \Exception(__('Please correct the tab configuration and try again. Tab Id should be not empty'));
119  }
120 
121  if (is_array($tab)) {
122  $this->_tabs[$tabId] = new \Magento\Framework\DataObject($tab);
123  } elseif ($tab instanceof \Magento\Framework\DataObject) {
124  $this->_tabs[$tabId] = $tab;
125  if (!$this->_tabs[$tabId]->hasTabId()) {
126  $this->_tabs[$tabId]->setTabId($tabId);
127  }
128  } elseif (is_string($tab)) {
129  $this->_addTabByName($tab, $tabId);
130 
131  if (!$this->_tabs[$tabId] instanceof TabInterface) {
132  unset($this->_tabs[$tabId]);
133  return $this;
134  }
135  } else {
136  throw new \Exception(__('Please correct the tab configuration and try again.'));
137  }
138 
139  if ($this->_tabs[$tabId]->getUrl() === null) {
140  $this->_tabs[$tabId]->setUrl('#');
141  }
142 
143  if (!$this->_tabs[$tabId]->getTitle()) {
144  $this->_tabs[$tabId]->setTitle($this->_tabs[$tabId]->getLabel());
145  }
146 
147  $this->_tabs[$tabId]->setId($tabId);
148  $this->_tabs[$tabId]->setTabId($tabId);
149 
150  if (true === $this->_tabs[$tabId]->getActive()) {
151  $this->setActiveTab($tabId);
152  }
153 
154  return $this;
155  }
156 
165  protected function _addTabByName($tab, $tabId)
166  {
167  if (strpos($tab, '\Block\\') !== false) {
168  $this->_tabs[$tabId] = $this->getLayout()->createBlock($tab, $this->getNameInLayout() . '_tab_' . $tabId);
169  } elseif ($this->getChildBlock($tab)) {
170  $this->_tabs[$tabId] = $this->getChildBlock($tab);
171  } else {
172  $this->_tabs[$tabId] = null;
173  }
174 
175  if ($this->_tabs[$tabId] !== null && !$this->_tabs[$tabId] instanceof TabInterface) {
176  throw new \Exception(__('Please correct the tab configuration and try again.'));
177  }
178  }
179 
183  public function getActiveTabId()
184  {
185  return $this->getTabId($this->_tabs[$this->_activeTab]);
186  }
187 
195  public function setActiveTab($tabId)
196  {
197  if (isset(
198  $this->_tabs[$tabId]
199  ) && $this->canShowTab(
200  $this->_tabs[$tabId]
201  ) && !$this->getTabIsHidden(
202  $this->_tabs[$tabId]
203  )
204  ) {
205  $this->_activeTab = $tabId;
206  if ($this->_activeTab !== null && $tabId !== $this->_activeTab) {
207  foreach ($this->_tabs as $id => $tab) {
208  $tab->setActive($id === $tabId);
209  }
210  }
211  }
212  return $this;
213  }
214 
221  protected function _setActiveTab($tabId)
222  {
223  foreach ($this->_tabs as $id => $tab) {
224  if ($this->getTabId($tab) == $tabId) {
225  $this->_activeTab = $id;
226  $tab->setActive(true);
227  return $this;
228  }
229  }
230  return $this;
231  }
232 
236  protected function _beforeToHtml()
237  {
238  $this->_tabs = $this->reorderTabs();
239 
240  if ($activeTab = $this->getRequest()->getParam('active_tab')) {
241  $this->setActiveTab($activeTab);
242  } elseif ($activeTabId = $this->_authSession->getActiveTabId()) {
243  $this->_setActiveTab($activeTabId);
244  }
245 
246  if ($this->_activeTab === null && !empty($this->_tabs)) {
248  $this->_activeTab = (reset($this->_tabs))->getId();
249  }
250 
251  $this->assign('tabs', $this->_tabs);
252  return parent::_beforeToHtml();
253  }
254 
260  private function reorderTabs()
261  {
262  $orderByIdentity = [];
263  $orderByPosition = [];
264  $position = 100;
265 
272  foreach ($this->_tabs as $key => $tab) {
273  $tab->setPosition($position);
274 
275  $orderByIdentity[$key] = $tab;
276  $orderByPosition[$position] = $tab;
277 
278  $position += 100;
279  }
280 
281  return $this->applyTabsCorrectOrder($orderByPosition, $orderByIdentity);
282  }
283 
290  private function applyTabsCorrectOrder(array $orderByPosition, array $orderByIdentity)
291  {
292  $positionFactor = 1;
293 
300  foreach ($orderByPosition as $position => $tab) {
301  if (!$tab->getAfter() || !in_array($tab->getAfter(), array_keys($orderByIdentity))) {
302  $positionFactor = 1;
303  continue;
304  }
305 
306  $grandPosition = $orderByIdentity[$tab->getAfter()]->getPosition();
307  $newPosition = $grandPosition + $positionFactor;
308 
309  unset($orderByPosition[$position]);
310  $orderByPosition[$newPosition] = $tab;
311  $tab->setPosition($newPosition);
312 
313  $positionFactor++;
314  }
315 
316  return $this->finalTabsSortOrder($orderByPosition);
317  }
318 
326  private function finalTabsSortOrder(array $orderByPosition)
327  {
328  ksort($orderByPosition);
329 
330  $ordered = [];
331 
333  foreach ($orderByPosition as $tab) {
334  $ordered[$tab->getId()] = $tab;
335  }
336 
337  return $ordered;
338  }
339 
343  public function getJsObjectName()
344  {
345  return $this->getId() . 'JsTabs';
346  }
347 
351  public function getTabsIds()
352  {
353  if (empty($this->_tabs)) {
354  return [];
355  }
356 
357  return array_keys($this->_tabs);
358  }
359 
365  public function getTabId($tab, $withPrefix = true)
366  {
367  if ($tab instanceof TabInterface) {
368  return ($withPrefix ? $this->getId() . '_' : '') . $tab->getTabId();
369  }
370  return ($withPrefix ? $this->getId() . '_' : '') . $tab->getId();
371  }
372 
377  public function canShowTab($tab)
378  {
379  if ($tab instanceof TabInterface) {
380  return $tab->canShowTab();
381  }
382  return true;
383  }
384 
390  public function getTabIsHidden($tab)
391  {
392  if ($tab instanceof TabInterface) {
393  return $tab->isHidden();
394  }
395  return $tab->getIsHidden();
396  }
397 
402  public function getTabUrl($tab)
403  {
404  if ($tab instanceof TabInterface) {
405  if (method_exists($tab, 'getTabUrl')) {
406  return $tab->getTabUrl();
407  }
408  return '#';
409  }
410  if ($tab->getUrl() !== null) {
411  return $tab->getUrl();
412  }
413  return '#';
414  }
415 
420  public function getTabTitle($tab)
421  {
422  if ($tab instanceof TabInterface) {
423  return $tab->getTabTitle();
424  }
425  return $tab->getTitle();
426  }
427 
432  public function getTabClass($tab)
433  {
434  if ($tab instanceof TabInterface) {
435  if (method_exists($tab, 'getTabClass')) {
436  return $tab->getTabClass();
437  }
438  return '';
439  }
440  return $tab->getClass();
441  }
442 
447  public function getTabLabel($tab)
448  {
449  if ($tab instanceof TabInterface) {
450  return $tab->getTabLabel();
451  }
452  return $tab->getLabel();
453  }
454 
459  public function getTabContent($tab)
460  {
461  if ($tab instanceof TabInterface) {
462  if ($tab->getSkipGenerateContent()) {
463  return '';
464  }
465  return $tab->toHtml();
466  }
467  return $tab->getContent();
468  }
469 
479  public function bindShadowTabs($tabOneId, $tabTwoId)
480  {
481  $tabs = [];
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;
487  }
488  }
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', []);
495  }
496  $this->_tabs[$tabToId]->setData(
497  'shadow_tabs',
498  array_merge($this->_tabs[$tabToId]->getData('shadow_tabs'), [$blockId . '_' . $tabId])
499  );
500  }
501  }
502  }
503  }
504  }
505 
512  public function getAllShadowTabs($asJson = true)
513  {
514  $result = [];
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');
520  }
521  }
522  }
523  if ($asJson) {
524  return $this->_jsonEncoder->encode($result);
525  }
526  return $result;
527  }
528 
537  public function setTabData($tab, $key, $value)
538  {
539  if (isset($this->_tabs[$tab]) && $this->_tabs[$tab] instanceof \Magento\Framework\DataObject) {
540  if ($key == 'url') {
541  $value = $this->getUrl($value, ['_current' => true, '_use_rewrite' => true]);
542  }
543  $this->_tabs[$tab]->setData($key, $value);
544  }
545 
546  return $this;
547  }
548 
555  public function removeTab($tabId)
556  {
557  if (isset($this->_tabs[$tabId])) {
558  unset($this->_tabs[$tabId]);
559  }
560  return $this;
561  }
562 }
getTabId($tab, $withPrefix=true)
Definition: Tabs.php:365
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
addTabAfter($tabId, $tab, $afterTabId)
Definition: Tabs.php:100
$value
Definition: gender.phtml:16
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Backend\Model\Auth\Session $authSession, array $data=[])
Definition: Tabs.php:59
setTabData($tab, $key, $value)
Definition: Tabs.php:537
bindShadowTabs($tabOneId, $tabTwoId)
Definition: Tabs.php:479
getAllShadowTabs($asJson=true)
Definition: Tabs.php:512