I am trying to check for the routename and based on that i want to display the top menu item as active or not. But for some reason is_active does not do anything. Even when i jusst straight up set it to false it still displays as active on the url given in the ‘url’. i want it to only be active when i am on a page where the route name is ‘posts’.
<?php
namespace DeVriesWebsolutionsPostsPluginBlock;
use MagentoFrameworkDataTreeNodeFactory;
use MagentoFrameworkUrlInterface;
use MagentoFrameworkAppRequestHttp;
class Topmenu
{
/**
* @var NodeFactory
*/
protected $nodeFactory;
public function __construct(
NodeFactory $nodeFactory,
UrlInterface $urlBuilder,
Http $request
) {
$this->nodeFactory = $nodeFactory;
$this->_urlBuilder = $urlBuilder;
$this->_request = $request;
}
public function beforeGetHtml(
MagentoThemeBlockHtmlTopmenu $subject,
$outermostClass = '',
$childrenWrapClass = '',
$limit = 0
) {
$node = $this->nodeFactory->create(
[
'data' => $this->getNodeAsArray(),
'idField' => 'id',
'tree' => $subject->getMenu()->getTree()
]
);
$subject->getMenu()->addChild($node);
}
protected function getNodeAsArray()
{
$routeName = $this->_request->getRouteName();
$url = $this->_urlBuilder->getUrl("posts/page");
$isActive = ($routeName == 'posts') ? true : false;
return [
'name' => __('Posts'),
'id' => 'posts_node_id',
'url' => $url,
'is_active' => $isActive
];
}
}
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="MagentoThemeBlockHtmlTopmenu">
<plugin name="posts-topmenu" type="DeVriesWebsolutionsPostsPluginBlockTopmenu" />
</type>
</config>