Cannot add custom topmenu link with plugin












0















I want to add custom links to the topmen / a submenu in the topmenu (I need the links for custom filters). To achieve this, I have created a plugin:



app/code/Vendor/Module/etc/frontend/di.xml:



<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<!-- Plugin to add custom topmenu links -->
<type name="MagentoThemeBlockHtmlTopmenu">
<plugin name="Module-topmenu" type="VendorModulePluginBlockTopmenu" sortOrder="1"/>
</type>
</config>


app/code/Vendor/Module/Plugin/Block/Topmenu.php:



namespace VendorModulePluginBlock;


class Topmenu
{
/**
* @var NodeFactory
*/
protected $nodeFactory;

public function __construct(
NodeFactory $nodeFactory
) {
$this->nodeFactory = $nodeFactory;
}

public function beforeGetHtml(
MagentoThemeBlockHtmlTopmenu $subject,
$outermostClass = '',
$childrenWrapClass = '',
$limit = 0
) {

$node = $this->nodeFactory->create(
[
'data' => $this->getNodeAsArray(),
'idField' => 'unique-id',
'tree' => $subject->getMenu()->getTree()
]
);

$subject->getMenu()->addChild($node);
}

protected function getNodeAsArray()
{
return [
'name' => 'Testlink',
'id' => 'testlink',
'url' => 'http://www.example.com/',
'has_active' => false,
'is_active' => false // (expression to determine if menu item is selected or not)
];
}
}


But this does not work, the custom menu link won't appear. Is there an error in my code, or is there another way to add topmenu/submenu links that are not categories?










share|improve this question



























    0















    I want to add custom links to the topmen / a submenu in the topmenu (I need the links for custom filters). To achieve this, I have created a plugin:



    app/code/Vendor/Module/etc/frontend/di.xml:



    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <!-- Plugin to add custom topmenu links -->
    <type name="MagentoThemeBlockHtmlTopmenu">
    <plugin name="Module-topmenu" type="VendorModulePluginBlockTopmenu" sortOrder="1"/>
    </type>
    </config>


    app/code/Vendor/Module/Plugin/Block/Topmenu.php:



    namespace VendorModulePluginBlock;


    class Topmenu
    {
    /**
    * @var NodeFactory
    */
    protected $nodeFactory;

    public function __construct(
    NodeFactory $nodeFactory
    ) {
    $this->nodeFactory = $nodeFactory;
    }

    public function beforeGetHtml(
    MagentoThemeBlockHtmlTopmenu $subject,
    $outermostClass = '',
    $childrenWrapClass = '',
    $limit = 0
    ) {

    $node = $this->nodeFactory->create(
    [
    'data' => $this->getNodeAsArray(),
    'idField' => 'unique-id',
    'tree' => $subject->getMenu()->getTree()
    ]
    );

    $subject->getMenu()->addChild($node);
    }

    protected function getNodeAsArray()
    {
    return [
    'name' => 'Testlink',
    'id' => 'testlink',
    'url' => 'http://www.example.com/',
    'has_active' => false,
    'is_active' => false // (expression to determine if menu item is selected or not)
    ];
    }
    }


    But this does not work, the custom menu link won't appear. Is there an error in my code, or is there another way to add topmenu/submenu links that are not categories?










    share|improve this question

























      0












      0








      0








      I want to add custom links to the topmen / a submenu in the topmenu (I need the links for custom filters). To achieve this, I have created a plugin:



      app/code/Vendor/Module/etc/frontend/di.xml:



      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
      <!-- Plugin to add custom topmenu links -->
      <type name="MagentoThemeBlockHtmlTopmenu">
      <plugin name="Module-topmenu" type="VendorModulePluginBlockTopmenu" sortOrder="1"/>
      </type>
      </config>


      app/code/Vendor/Module/Plugin/Block/Topmenu.php:



      namespace VendorModulePluginBlock;


      class Topmenu
      {
      /**
      * @var NodeFactory
      */
      protected $nodeFactory;

      public function __construct(
      NodeFactory $nodeFactory
      ) {
      $this->nodeFactory = $nodeFactory;
      }

      public function beforeGetHtml(
      MagentoThemeBlockHtmlTopmenu $subject,
      $outermostClass = '',
      $childrenWrapClass = '',
      $limit = 0
      ) {

      $node = $this->nodeFactory->create(
      [
      'data' => $this->getNodeAsArray(),
      'idField' => 'unique-id',
      'tree' => $subject->getMenu()->getTree()
      ]
      );

      $subject->getMenu()->addChild($node);
      }

      protected function getNodeAsArray()
      {
      return [
      'name' => 'Testlink',
      'id' => 'testlink',
      'url' => 'http://www.example.com/',
      'has_active' => false,
      'is_active' => false // (expression to determine if menu item is selected or not)
      ];
      }
      }


      But this does not work, the custom menu link won't appear. Is there an error in my code, or is there another way to add topmenu/submenu links that are not categories?










      share|improve this question














      I want to add custom links to the topmen / a submenu in the topmenu (I need the links for custom filters). To achieve this, I have created a plugin:



      app/code/Vendor/Module/etc/frontend/di.xml:



      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
      <!-- Plugin to add custom topmenu links -->
      <type name="MagentoThemeBlockHtmlTopmenu">
      <plugin name="Module-topmenu" type="VendorModulePluginBlockTopmenu" sortOrder="1"/>
      </type>
      </config>


      app/code/Vendor/Module/Plugin/Block/Topmenu.php:



      namespace VendorModulePluginBlock;


      class Topmenu
      {
      /**
      * @var NodeFactory
      */
      protected $nodeFactory;

      public function __construct(
      NodeFactory $nodeFactory
      ) {
      $this->nodeFactory = $nodeFactory;
      }

      public function beforeGetHtml(
      MagentoThemeBlockHtmlTopmenu $subject,
      $outermostClass = '',
      $childrenWrapClass = '',
      $limit = 0
      ) {

      $node = $this->nodeFactory->create(
      [
      'data' => $this->getNodeAsArray(),
      'idField' => 'unique-id',
      'tree' => $subject->getMenu()->getTree()
      ]
      );

      $subject->getMenu()->addChild($node);
      }

      protected function getNodeAsArray()
      {
      return [
      'name' => 'Testlink',
      'id' => 'testlink',
      'url' => 'http://www.example.com/',
      'has_active' => false,
      'is_active' => false // (expression to determine if menu item is selected or not)
      ];
      }
      }


      But this does not work, the custom menu link won't appear. Is there an error in my code, or is there another way to add topmenu/submenu links that are not categories?







      magento2 magento-2.1 module plugin topmenu






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 23 mins ago









      DorniDorni

      115




      115






















          1 Answer
          1






          active

          oldest

          votes


















          0














          First, create a module.



          Second: create di.xml where we will define the plugin.




          appcode{VendorName}{ModuleName}etcdi.xml




          <?xml version="1.0"?>
          <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <type name="MagentoThemeBlockHtmlTopmenu">
          <plugin name="add_cms_menu" type="{VendorName}{ModuleName}PluginTopmenu" sortOrder="1" />
          </type>
          </config>


          3.Now create the plugin class where we will add a cms page link to the menu.




          File Path: appcode{VendorName}{ModuleName}PluginTopmenu.php




          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;
          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }
          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          /* Showing Cms page About us at menu */
          $page = $this->getCmspage('about-us');
          if($page == null){
          return;
          }


          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => $page->getTitle(),
          'id' => $page->getIdentifier(),
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]),
          'has_active' => false,
          'is_active' => false // (expression to determine if menu item is selected or not)
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }
          protected function getCmspage($identifier){

          $page = $this->_pageFactory->create();
          $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());

          if (!$pageId) {
          return null;
          }
          $page->setStoreId($this->_storeManager->getStore()->getId());
          if (!$page->load($pageId)) {
          return null;
          }

          if (!$page->getId()) {
          return null;
          }

          return $page;
          }

          }


          In this way, you can Magento2 an add custom link to menu or navigation.



          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;

          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }

          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          // Add contact us page in menu

          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => __('CONTACT US'),
          'id' => 'contactus',
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' =>'contact']),
          'has_active' => false,
          'is_active' => false
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }


          }


          This module should have below files:




          app/code/{VendorName}/{ModuleName}/etc/module.xml
          app/code/{VendorName}/{ModuleName}/composer.json
          app/code/{VendorName}/{ModuleName}/registration.json







          share|improve this answer


























          • Thank you for the help, but this won't allow me to add links to existing submenus of categories. I'd need to add custom links to an already existing submenu which contains categories.

            – Dorni
            9 mins ago











          • please check my code now i have updated my code you can add now your custom link easily

            – Rakesh Donga
            6 mins ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258114%2fcannot-add-custom-topmenu-link-with-plugin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          First, create a module.



          Second: create di.xml where we will define the plugin.




          appcode{VendorName}{ModuleName}etcdi.xml




          <?xml version="1.0"?>
          <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <type name="MagentoThemeBlockHtmlTopmenu">
          <plugin name="add_cms_menu" type="{VendorName}{ModuleName}PluginTopmenu" sortOrder="1" />
          </type>
          </config>


          3.Now create the plugin class where we will add a cms page link to the menu.




          File Path: appcode{VendorName}{ModuleName}PluginTopmenu.php




          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;
          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }
          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          /* Showing Cms page About us at menu */
          $page = $this->getCmspage('about-us');
          if($page == null){
          return;
          }


          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => $page->getTitle(),
          'id' => $page->getIdentifier(),
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]),
          'has_active' => false,
          'is_active' => false // (expression to determine if menu item is selected or not)
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }
          protected function getCmspage($identifier){

          $page = $this->_pageFactory->create();
          $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());

          if (!$pageId) {
          return null;
          }
          $page->setStoreId($this->_storeManager->getStore()->getId());
          if (!$page->load($pageId)) {
          return null;
          }

          if (!$page->getId()) {
          return null;
          }

          return $page;
          }

          }


          In this way, you can Magento2 an add custom link to menu or navigation.



          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;

          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }

          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          // Add contact us page in menu

          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => __('CONTACT US'),
          'id' => 'contactus',
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' =>'contact']),
          'has_active' => false,
          'is_active' => false
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }


          }


          This module should have below files:




          app/code/{VendorName}/{ModuleName}/etc/module.xml
          app/code/{VendorName}/{ModuleName}/composer.json
          app/code/{VendorName}/{ModuleName}/registration.json







          share|improve this answer


























          • Thank you for the help, but this won't allow me to add links to existing submenus of categories. I'd need to add custom links to an already existing submenu which contains categories.

            – Dorni
            9 mins ago











          • please check my code now i have updated my code you can add now your custom link easily

            – Rakesh Donga
            6 mins ago
















          0














          First, create a module.



          Second: create di.xml where we will define the plugin.




          appcode{VendorName}{ModuleName}etcdi.xml




          <?xml version="1.0"?>
          <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <type name="MagentoThemeBlockHtmlTopmenu">
          <plugin name="add_cms_menu" type="{VendorName}{ModuleName}PluginTopmenu" sortOrder="1" />
          </type>
          </config>


          3.Now create the plugin class where we will add a cms page link to the menu.




          File Path: appcode{VendorName}{ModuleName}PluginTopmenu.php




          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;
          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }
          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          /* Showing Cms page About us at menu */
          $page = $this->getCmspage('about-us');
          if($page == null){
          return;
          }


          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => $page->getTitle(),
          'id' => $page->getIdentifier(),
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]),
          'has_active' => false,
          'is_active' => false // (expression to determine if menu item is selected or not)
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }
          protected function getCmspage($identifier){

          $page = $this->_pageFactory->create();
          $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());

          if (!$pageId) {
          return null;
          }
          $page->setStoreId($this->_storeManager->getStore()->getId());
          if (!$page->load($pageId)) {
          return null;
          }

          if (!$page->getId()) {
          return null;
          }

          return $page;
          }

          }


          In this way, you can Magento2 an add custom link to menu or navigation.



          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;

          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }

          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          // Add contact us page in menu

          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => __('CONTACT US'),
          'id' => 'contactus',
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' =>'contact']),
          'has_active' => false,
          'is_active' => false
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }


          }


          This module should have below files:




          app/code/{VendorName}/{ModuleName}/etc/module.xml
          app/code/{VendorName}/{ModuleName}/composer.json
          app/code/{VendorName}/{ModuleName}/registration.json







          share|improve this answer


























          • Thank you for the help, but this won't allow me to add links to existing submenus of categories. I'd need to add custom links to an already existing submenu which contains categories.

            – Dorni
            9 mins ago











          • please check my code now i have updated my code you can add now your custom link easily

            – Rakesh Donga
            6 mins ago














          0












          0








          0







          First, create a module.



          Second: create di.xml where we will define the plugin.




          appcode{VendorName}{ModuleName}etcdi.xml




          <?xml version="1.0"?>
          <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <type name="MagentoThemeBlockHtmlTopmenu">
          <plugin name="add_cms_menu" type="{VendorName}{ModuleName}PluginTopmenu" sortOrder="1" />
          </type>
          </config>


          3.Now create the plugin class where we will add a cms page link to the menu.




          File Path: appcode{VendorName}{ModuleName}PluginTopmenu.php




          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;
          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }
          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          /* Showing Cms page About us at menu */
          $page = $this->getCmspage('about-us');
          if($page == null){
          return;
          }


          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => $page->getTitle(),
          'id' => $page->getIdentifier(),
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]),
          'has_active' => false,
          'is_active' => false // (expression to determine if menu item is selected or not)
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }
          protected function getCmspage($identifier){

          $page = $this->_pageFactory->create();
          $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());

          if (!$pageId) {
          return null;
          }
          $page->setStoreId($this->_storeManager->getStore()->getId());
          if (!$page->load($pageId)) {
          return null;
          }

          if (!$page->getId()) {
          return null;
          }

          return $page;
          }

          }


          In this way, you can Magento2 an add custom link to menu or navigation.



          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;

          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }

          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          // Add contact us page in menu

          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => __('CONTACT US'),
          'id' => 'contactus',
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' =>'contact']),
          'has_active' => false,
          'is_active' => false
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }


          }


          This module should have below files:




          app/code/{VendorName}/{ModuleName}/etc/module.xml
          app/code/{VendorName}/{ModuleName}/composer.json
          app/code/{VendorName}/{ModuleName}/registration.json







          share|improve this answer















          First, create a module.



          Second: create di.xml where we will define the plugin.




          appcode{VendorName}{ModuleName}etcdi.xml




          <?xml version="1.0"?>
          <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <type name="MagentoThemeBlockHtmlTopmenu">
          <plugin name="add_cms_menu" type="{VendorName}{ModuleName}PluginTopmenu" sortOrder="1" />
          </type>
          </config>


          3.Now create the plugin class where we will add a cms page link to the menu.




          File Path: appcode{VendorName}{ModuleName}PluginTopmenu.php




          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;
          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }
          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          /* Showing Cms page About us at menu */
          $page = $this->getCmspage('about-us');
          if($page == null){
          return;
          }


          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => $page->getTitle(),
          'id' => $page->getIdentifier(),
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]),
          'has_active' => false,
          'is_active' => false // (expression to determine if menu item is selected or not)
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }
          protected function getCmspage($identifier){

          $page = $this->_pageFactory->create();
          $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());

          if (!$pageId) {
          return null;
          }
          $page->setStoreId($this->_storeManager->getStore()->getId());
          if (!$page->load($pageId)) {
          return null;
          }

          if (!$page->getId()) {
          return null;
          }

          return $page;
          }

          }


          In this way, you can Magento2 an add custom link to menu or navigation.



          <?php 
          namespace {VendorName}{ModuleName}Plugin;
          use MagentoFrameworkDataTreeNodeFactory;

          class Topmenu
          {
          protected $nodeFactory;
          protected $_storeManager;
          protected $_pageFactory;
          protected $_urlBuilder;

          public function __construct(
          NodeFactory $nodeFactory,
          MagentoCmsModelPageFactory $pageFactory,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoFrameworkUrlInterface $urlBuilder
          ) {
          $this->nodeFactory = $nodeFactory;
          $this->_pageFactory = $pageFactory;
          $this->_storeManager = $storeManager;
          $this->_urlBuilder = $urlBuilder;
          }

          public function beforeGetHtml(
          MagentoThemeBlockHtmlTopmenu $subject,
          $outermostClass = '',
          $childrenWrapClass = '',
          $limit = 0
          ) {
          // Add contact us page in menu

          $node = $this->nodeFactory->create(
          [
          'data' => [
          'name' => __('CONTACT US'),
          'id' => 'contactus',
          'url' => $this->_urlBuilder->getUrl(null, ['_direct' =>'contact']),
          'has_active' => false,
          'is_active' => false
          ],
          'idField' => 'id',
          'tree' => $subject->getMenu()->getTree()
          ]
          );
          $subject->getMenu()->addChild($node);
          }


          }


          This module should have below files:




          app/code/{VendorName}/{ModuleName}/etc/module.xml
          app/code/{VendorName}/{ModuleName}/composer.json
          app/code/{VendorName}/{ModuleName}/registration.json








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 7 mins ago

























          answered 19 mins ago









          Rakesh DongaRakesh Donga

          865




          865













          • Thank you for the help, but this won't allow me to add links to existing submenus of categories. I'd need to add custom links to an already existing submenu which contains categories.

            – Dorni
            9 mins ago











          • please check my code now i have updated my code you can add now your custom link easily

            – Rakesh Donga
            6 mins ago



















          • Thank you for the help, but this won't allow me to add links to existing submenus of categories. I'd need to add custom links to an already existing submenu which contains categories.

            – Dorni
            9 mins ago











          • please check my code now i have updated my code you can add now your custom link easily

            – Rakesh Donga
            6 mins ago

















          Thank you for the help, but this won't allow me to add links to existing submenus of categories. I'd need to add custom links to an already existing submenu which contains categories.

          – Dorni
          9 mins ago





          Thank you for the help, but this won't allow me to add links to existing submenus of categories. I'd need to add custom links to an already existing submenu which contains categories.

          – Dorni
          9 mins ago













          please check my code now i have updated my code you can add now your custom link easily

          – Rakesh Donga
          6 mins ago





          please check my code now i have updated my code you can add now your custom link easily

          – Rakesh Donga
          6 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258114%2fcannot-add-custom-topmenu-link-with-plugin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What other Star Trek series did the main TNG cast show up in?

          Berlina muro

          Berlina aerponto