1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
*/
namespace Magefan\Blog\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Data\Tree\Node;
/**
* Blog observer
*/
class PageBlockHtmlTopmenuBethtmlBeforeObserver implements ObserverInterface
{
/**
* Show top menu item config path
*/
const XML_PATH_TOP_MENU_SHOW_ITEM = 'mfblog/top_menu/show_item';
/**
* Top menu item text config path
*/
const XML_PATH_TOP_MENU_ITEM_TEXT = 'mfblog/top_menu/item_text';
/**
* @var \Magefan\Blog\Model\Url
*/
protected $_url;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* @param \Magefan\Blog\Model\Url $url
*/
public function __construct(
\Magefan\Blog\Model\Url $url,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->_scopeConfig = $scopeConfig;
$this->_url = $url;
}
/**
* Page block html topmenu gethtml before
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if (!$this->_scopeConfig->isSetFlag(static::XML_PATH_TOP_MENU_SHOW_ITEM, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
return;
}
/** @var \Magento\Framework\Data\Tree\Node $menu */
$menu = $observer->getMenu();
$block = $observer->getBlock();
$tree = $menu->getTree();
$data = [
'name' => $this->_scopeConfig->getValue(static::XML_PATH_TOP_MENU_ITEM_TEXT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
'id' => 'magefan-blog',
'url' => $this->_url->getBaseUrl(),
'is_active' => ($block->getRequest()->getModuleName() == 'blog'),
];
$node = new Node($data, 'id', $tree, $menu);
$menu->addChild($node);
}
}