Magento – Add Custom Header Link in Magento 2 – Not Working

headermagento2

Here's my code:

default.xml file in app/code/Demo/Mymodule/view/frontend/layout

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="header.links">
            <block class=”Demo\Mymodule\Block\Link" name="custom-header-link" >
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">test</argument>
                    <argument name="path" xsi:type="string" translate="true">Test</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Link.php in app/code/Demo/Mymodule/Block:

<?php

namespace Demo\Mymodule\Block;

class Link extends \Magento\Framework\View\Element\Html\Link
{
/**
* Render block HTML.
*
* @return string
*/
protected function _toHtml()
    {
     if (false != $this->getTemplate()) {
     return parent::_toHtml();
     }
     return '<li><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '</a></li>';
    }
}

But it's not showing!

Best Answer

Step 1:

Ex: Magento 2 root directory/app/code/Demo/Mymodule/

Step 2:

Create module.xml file in the below path to define your Magento 2 extension.

Magento 2 root directory/app/code/Demo/Mymodule/etc/module.xml

<?xml version=“1.0”?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Demo_Mymodule" setup_version="2.0.1"></module>
</config>

Step 3:

Create routes.xml in the below path for your custom link.

Magento 2 root directory/app/Demo/Mymodule/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="newlink" frontName="newlink">
            <module name="Demo_Mymodule" />
        </route>
    </router>
</config>

Step 4:

Create layout file in the below path for a custom header.

Magento2 root directory/app/Demo/Mymodule/view/frontend/layout/header.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="header.links">
            <!-- <move element="test-last" destination="header.links"/> -->
            <block class="Demo\Mymodule\Block\Header" name="test-link" after="my-account-link"/>
        </referenceBlock>
    </body>
</page>

Step 5:

Create block file that is referred in the layout file in the below path.

Magento2 root directory/app/Demo/Mymodule/Block/Header.php

<?php
namespace Demo\Mymodule\Block;
class Header extends \Magento\Framework\View\Element\Html\Link{

    protected $_template = 'Demo_Mymodule::link.phtml';

    public function getHref(){
        return __( 'testuser');
    }

    public function getLabel(){
        return __('Test Link');
    }
}
?>

Step 6:

Create template file that is referred to the block file in the below path.

Magento2 root directory/app/Demo/Mymodule/frontend/templates/link.phtml

<li>
    <a <?php echo $block->getLinkAttributes() ?>><?php echo $block->escapeHtml($block->getLabel())?>
        <?php echo($block->getCounter()) ? '<span>' . $block->escapeHtml($block->getCounter()) . '</span>' : ''; ?>
    </a>
</li>
Related Topic