Skip to content

Commit 06dd04b

Browse files
author
Volodymyr Klymenko
authored
Merge pull request #799 from magento-firedrakes/MAGETWO-63716
[Firedrakes] Add target to admin menu element
2 parents 061977e + 2a22a46 commit 06dd04b

File tree

20 files changed

+907
-110
lines changed

20 files changed

+907
-110
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Backend\Block;
7+
8+
use Magento\Backend\Model\Menu\Item;
9+
use Magento\Framework\Escaper;
10+
11+
/**
12+
* Class AnchorRenderer
13+
*/
14+
class AnchorRenderer
15+
{
16+
/**
17+
* @var MenuItemChecker
18+
*/
19+
private $menuItemChecker;
20+
21+
/**
22+
* @var Escaper
23+
*/
24+
private $escaper;
25+
26+
/**
27+
* @param MenuItemChecker $menuItemChecker
28+
* @param Escaper $escaper
29+
*/
30+
public function __construct(
31+
MenuItemChecker $menuItemChecker,
32+
Escaper $escaper
33+
) {
34+
$this->menuItemChecker = $menuItemChecker;
35+
$this->escaper = $escaper;
36+
}
37+
38+
/**
39+
* Render menu item anchor.
40+
*
41+
* It is used in backend menu to render anchor menu.
42+
*
43+
* @param Item|false $activeItem Can be false if menu item is inaccessible
44+
* but was triggered directly using controller. It is a legacy code behaviour.
45+
* @param Item $menuItem
46+
* @param int $level
47+
* @return string
48+
*/
49+
public function renderAnchor($activeItem, Item $menuItem, $level)
50+
{
51+
if ($level == 1 && $menuItem->getUrl() == '#') {
52+
$output = '<strong class="submenu-group-title" role="presentation">'
53+
. '<span>' . $this->escaper->escapeHtml(__($menuItem->getTitle())) . '</span>'
54+
. '</strong>';
55+
} else {
56+
$target = $menuItem->getTarget() ? ('target=' . $menuItem->getTarget()) : '';
57+
$output = '<a href="' . $menuItem->getUrl() . '" ' . $target . ' ' . $this->_renderItemAnchorTitle(
58+
$menuItem
59+
) . $this->_renderItemOnclickFunction(
60+
$menuItem
61+
) . ' class="' . ($this->menuItemChecker->isItemActive($activeItem, $menuItem, $level) ? '_active' : '')
62+
. '">' . '<span>' . $this->escaper->escapeHtml(__($menuItem->getTitle()))
63+
. '</span>' . '</a>';
64+
}
65+
66+
return $output;
67+
}
68+
69+
/**
70+
* Render menu item anchor title
71+
*
72+
* @param Item $menuItem
73+
* @return string
74+
*/
75+
private function _renderItemAnchorTitle($menuItem)
76+
{
77+
return $menuItem->hasTooltip() ? 'title="' . __($menuItem->getTooltip()) . '"' : '';
78+
}
79+
80+
/**
81+
* Render menu item onclick function
82+
*
83+
* @param Item $menuItem
84+
* @return string
85+
*/
86+
private function _renderItemOnclickFunction($menuItem)
87+
{
88+
return $menuItem->hasClickCallback() ? ' onclick="' . $menuItem->getClickCallback() . '"' : '';
89+
}
90+
}

app/code/Magento/Backend/Block/Menu.php

Lines changed: 26 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Menu extends \Magento\Backend\Block\Template
3838
/**
3939
* Current selected item
4040
*
41-
* @var \Magento\Backend\Model\Menu\Item|null|bool
41+
* @var \Magento\Backend\Model\Menu\Item|false|null
4242
*/
4343
protected $_activeItemModel = null;
4444

@@ -62,6 +62,16 @@ class Menu extends \Magento\Backend\Block\Template
6262
*/
6363
protected $_localeResolver;
6464

65+
/**
66+
* @var MenuItemChecker
67+
*/
68+
private $menuItemChecker;
69+
70+
/**
71+
* @var AnchorRenderer
72+
*/
73+
private $anchorRenderer;
74+
6575
/**
6676
* @param Template\Context $context
6777
* @param \Magento\Backend\Model\UrlInterface $url
@@ -70,6 +80,8 @@ class Menu extends \Magento\Backend\Block\Template
7080
* @param \Magento\Backend\Model\Menu\Config $menuConfig
7181
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
7282
* @param array $data
83+
* @param MenuItemChecker|null $menuItemChecker
84+
* @param AnchorRenderer|null $anchorRenderer
7385
*/
7486
public function __construct(
7587
\Magento\Backend\Block\Template\Context $context,
@@ -78,13 +90,17 @@ public function __construct(
7890
\Magento\Backend\Model\Auth\Session $authSession,
7991
\Magento\Backend\Model\Menu\Config $menuConfig,
8092
\Magento\Framework\Locale\ResolverInterface $localeResolver,
81-
array $data = []
93+
array $data = [],
94+
MenuItemChecker $menuItemChecker = null,
95+
AnchorRenderer $anchorRenderer = null
8296
) {
8397
$this->_url = $url;
8498
$this->_iteratorFactory = $iteratorFactory;
8599
$this->_authSession = $authSession;
86100
$this->_menuConfig = $menuConfig;
87101
$this->_localeResolver = $localeResolver;
102+
$this->menuItemChecker = $menuItemChecker;
103+
$this->anchorRenderer = $anchorRenderer;
88104
parent::__construct($context, $data);
89105
}
90106

@@ -99,30 +115,6 @@ protected function _construct()
99115
$this->setCacheTags([self::CACHE_TAGS]);
100116
}
101117

102-
/**
103-
* Check whether given item is currently selected
104-
*
105-
* @param \Magento\Backend\Model\Menu\Item $item
106-
* @param int $level
107-
* @return bool
108-
*/
109-
protected function _isItemActive(\Magento\Backend\Model\Menu\Item $item, $level)
110-
{
111-
$itemModel = $this->getActiveItemModel();
112-
$output = false;
113-
114-
if ($level == 0 &&
115-
$itemModel instanceof \Magento\Backend\Model\Menu\Item &&
116-
($itemModel->getId() == $item->getId() ||
117-
$item->getChildren()->get(
118-
$itemModel->getId()
119-
) !== null)
120-
) {
121-
$output = true;
122-
}
123-
return $output;
124-
}
125-
126118
/**
127119
* Render menu item anchor label
128120
*
@@ -134,40 +126,6 @@ protected function _getAnchorLabel($menuItem)
134126
return $this->escapeHtml(__($menuItem->getTitle()));
135127
}
136128

137-
/**
138-
* Render menu item anchor title
139-
*
140-
* @param \Magento\Backend\Model\Menu\Item $menuItem
141-
* @return string
142-
*/
143-
protected function _renderItemAnchorTitle($menuItem)
144-
{
145-
return $menuItem->hasTooltip() ? 'title="' . __($menuItem->getTooltip()) . '"' : '';
146-
}
147-
148-
/**
149-
* Render menu item onclick function
150-
*
151-
* @param \Magento\Backend\Model\Menu\Item $menuItem
152-
* @return string
153-
*/
154-
protected function _renderItemOnclickFunction($menuItem)
155-
{
156-
return $menuItem->hasClickCallback() ? ' onclick="' . $menuItem->getClickCallback() . '"' : '';
157-
}
158-
159-
/**
160-
* Render menu item anchor css class
161-
*
162-
* @param \Magento\Backend\Model\Menu\Item $menuItem
163-
* @param int $level
164-
* @return string
165-
*/
166-
protected function _renderAnchorCssClass($menuItem, $level)
167-
{
168-
return $this->_isItemActive($menuItem, $level) ? '_active' : '';
169-
}
170-
171129
/**
172130
* Render menu item mouse events
173131
* @param \Magento\Backend\Model\Menu\Item $menuItem
@@ -188,10 +146,11 @@ protected function _renderMouseEvent($menuItem)
188146
protected function _renderItemCssClass($menuItem, $level)
189147
{
190148
$isLast = 0 == $level && (bool)$this->getMenuModel()->isLast($menuItem) ? 'last' : '';
191-
$output = ($this->_isItemActive(
192-
$menuItem,
193-
$level
194-
) ? '_current _active' : '') .
149+
$output = ($this->menuItemChecker->isItemActive(
150+
$this->getActiveItemModel(),
151+
$menuItem,
152+
$level
153+
) ? '_current _active' : '') .
195154
' ' .
196155
($menuItem->hasChildren() ? 'parent' : '') .
197156
' ' .
@@ -202,34 +161,6 @@ protected function _renderItemCssClass($menuItem, $level)
202161
return $output;
203162
}
204163

205-
/**
206-
* Render menu item anchor
207-
* @param \Magento\Backend\Model\Menu\Item $menuItem
208-
* @param int $level
209-
* @return string
210-
*/
211-
protected function _renderAnchor($menuItem, $level)
212-
{
213-
if ($level == 1 && $menuItem->getUrl() == '#') {
214-
$output = '<strong class="submenu-group-title" role="presentation">'
215-
. '<span>' . $this->_getAnchorLabel($menuItem) . '</span>'
216-
. '</strong>';
217-
} else {
218-
$output = '<a href="' . $menuItem->getUrl() . '" ' . $this->_renderItemAnchorTitle(
219-
$menuItem
220-
) . $this->_renderItemOnclickFunction(
221-
$menuItem
222-
) . ' class="' . $this->_renderAnchorCssClass(
223-
$menuItem,
224-
$level
225-
) . '">' . '<span>' . $this->_getAnchorLabel(
226-
$menuItem
227-
) . '</span>' . '</a>';
228-
}
229-
230-
return $output;
231-
}
232-
233164
/**
234165
* Get menu filter iterator
235166
*
@@ -336,7 +267,7 @@ public function renderMenu($menu, $level = 0)
336267
$menuItem->getId()
337268
) . 'role="menuitem">';
338269

339-
$output .= $this->_renderAnchor($menuItem, $level);
270+
$output .= $this->anchorRenderer->renderAnchor($this->getActiveItemModel(), $menuItem, $level);
340271

341272
if ($menuItem->hasChildren()) {
342273
$output .= $this->renderMenu($menuItem->getChildren(), $level + 1);
@@ -456,7 +387,7 @@ public function renderNavigation($menu, $level = 0, $limit = 0, $colBrakes = [])
456387

457388
$id = $this->getJsId($menuItem->getId());
458389
$subMenu = $this->_addSubMenu($menuItem, $level, $limit, $id);
459-
$anchor = $this->_renderAnchor($menuItem, $level);
390+
$anchor = $this->anchorRenderer->renderAnchor($this->getActiveItemModel(), $menuItem, $level);
460391
$output .= '<li ' . $this->getUiId($menuItem->getId())
461392
. ' class="item-' . $itemClass . ' ' . $this->_renderItemCssClass($menuItem, $level)
462393
. ($level == 0 ? '" id="' . $id . '" aria-haspopup="true' : '')
@@ -474,7 +405,7 @@ public function renderNavigation($menu, $level = 0, $limit = 0, $colBrakes = [])
474405
/**
475406
* Get current selected menu item
476407
*
477-
* @return \Magento\Backend\Model\Menu\Item|null|bool
408+
* @return \Magento\Backend\Model\Menu\Item|false
478409
*/
479410
public function getActiveItemModel()
480411
{
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Backend\Block;
7+
8+
use Magento\Backend\Model\Menu\Item;
9+
10+
/**
11+
* Class MenuItemChecker
12+
*/
13+
class MenuItemChecker
14+
{
15+
/**
16+
* Check whether given menu item is currently selected.
17+
*
18+
* It is used in backend menu to highlight active menu item.
19+
*
20+
* @param Item|false $activeItem Can be false if menu item is inaccessible
21+
* but was triggered directly using controller. It is a legacy code behaviour.
22+
* @param Item $item
23+
* @param int $level
24+
* @return bool
25+
*/
26+
public function isItemActive($activeItem, Item $item, $level)
27+
{
28+
$output = false;
29+
30+
if ($level == 0
31+
&& $activeItem instanceof \Magento\Backend\Model\Menu\Item
32+
&& $this->isActiveItemEqualOrChild($activeItem, $item)
33+
) {
34+
$output = true;
35+
}
36+
return $output;
37+
}
38+
39+
/**
40+
* @param Item $activeItem,
41+
* @param Item $item
42+
* @return bool
43+
*/
44+
private function isActiveItemEqualOrChild($activeItem, $item)
45+
{
46+
return ($activeItem->getId() == $item->getId())
47+
|| ($item->getChildren()->get($activeItem->getId()) !== null);
48+
}
49+
}

app/code/Magento/Backend/Controller/Adminhtml/System/Store/Index.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@
66
*/
77
namespace Magento\Backend\Controller\Adminhtml\System\Store;
88

9+
use Magento\Framework\Controller\ResultFactory;
10+
11+
/**
12+
* Class Index returns Stores page
13+
*/
914
class Index extends \Magento\Backend\Controller\Adminhtml\System\Store
1015
{
1116
/**
17+
* Returns Stores page
18+
*
1219
* @return \Magento\Backend\Model\View\Result\Page
1320
*/
1421
public function execute()
1522
{
16-
$resultPage = $this->resultPageFactory->create();
23+
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
24+
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
25+
$resultPage->setActiveMenu('Magento_Backend::system_store');
26+
$resultPage->addBreadcrumb(__('Stores'), __('Stores'));
27+
$resultPage->addBreadcrumb(__('All Stores'), __('All Stores'));
1728
$resultPage->getConfig()->getTitle()->prepend(__('Stores'));
18-
1929
return $resultPage;
2030
}
2131
}

app/code/Magento/Backend/Model/Menu/Config/Converter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
*/
66
namespace Magento\Backend\Model\Menu\Config;
77

8+
/**
9+
* Class Converter converts xml to appropriate array
10+
*/
811
class Converter implements \Magento\Framework\Config\ConverterInterface
912
{
1013
/**
14+
* Converts xml to appropriate array
15+
*
1116
* @param mixed $dom
1217
* @return array
1318
*/
@@ -26,6 +31,7 @@ public function convert($dom)
2631
'resource',
2732
'dependsOnModule',
2833
'dependsOnConfig',
34+
'target'
2935
];
3036
$xpath = new \DOMXPath($dom);
3137
$nodeList = $xpath->query('/config/menu/*');

0 commit comments

Comments
 (0)