0

I am quite new to Magento 2.0 and I feel a bit disappointed how it is hard to add simple page to top menu which should be one of the basics of CMS.

My page Title and URL key are 'collection'

I tried some solutions as adding this to my default.xml but didn't work.

<referenceContainer name="top.links">
           <block class="Magento\Framework\View\Element\Html\Link\Current" name="collection">
                <arguments>
                    <argument name="label" xsi:type="string">collection</argument>
                    <argument name="path" xsi:type="string">collection</argument>
                </arguments>
          </block> 
       </referenceContainer>

Am I missing something ? I don't want to make a module because its a bit advanced to me. Any help please

2

one alternative solution is that

create one new category and assign cms block to it

and add cms page code to that cms block

evenrything work as per you needed.

0
2

Without any extension,it's not possible. But there is an alternative

Basically, we use cms page for cms content management.

As per as my knowledge,there is a process in category system where you can manage cms content.

Just create a cms static block and assign that static block to category from Display setting tab.

Then,set Display Mode as Static block only. Do not assign any products to that category. If you donot want alternative then

Then create plugin for class Magento\Theme\Block\Html\Topmenu and and use before plugin on _getHtml()

First, create a module.

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

<?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="Magento\Theme\Block\Html\Topmenu">
        <plugin name="add_cms_menu" type="{VendorName}{ModuleName}\Plugin\Topmenu" sortOrder="1" />
    </type>
</config>

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

and also in this way we can add a cms page link at navigation.

app\code{VendorName}{ModuleName}\Plugin\Topmenu.php



<?php 
namespace {VendorName}\{ModuleName}\Plugin;
use Magento\Framework\Data\Tree\NodeFactory;
class Topmenu
{
    protected $nodeFactory;
    protected $_storeManager;
    protected $_pageFactory;
    protected $_urlBuilder;

    public function __construct(
        NodeFactory $nodeFactory,
        \Magento\Cms\Model\PageFactory $pageFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\UrlInterface $urlBuilder
    ) {
        $this->nodeFactory = $nodeFactory;
        $this->_pageFactory = $pageFactory;
        $this->_storeManager = $storeManager;
        $this->_urlBuilder = $urlBuilder;
    }
    public function beforeGetHtml(
        \Magento\Theme\Block\Html\Topmenu $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;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.