0

Want to show top links as "DROPDOWN" in custom theme. Trying from many days but still no success.

Have to show "Sign In/Up" Link in toplinks when customer is not logged in.

After Customer Logged In "Sign In/Up" replaced with Customer Name or Email Id with a "on hover" "DROPDOWN" having the following links in that

"My Account" "My Orders" "Custom Link" "Sign Out"

Please help me out.

Reference for the above question

Reference Image for the above links

  • did you get any solution ?? – Manish Aug 1 '18 at 10:34
0

You have to create a block class to achieve that.

First of all in your module's frontend default.xml layout file you have to add the following code

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="top.links">
            <block class="Your_Namespace\Your_Modulename\Block\Link" name="custom-link" after="my-account-link"/>
        </referenceBlock>
    </body>
</page>

Your block class

namespace Your_Namespace\Your_Modulename\Block;
/**
 * Class Link
 *
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 */
class Link extends \Magento\Framework\View\Element\Html\Link
{
    /**
     * Template name
     *
     * @var string
     */
    protected $_template = 'Your_Namespace_Your_Modulename::link.phtml';  

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    /**
     * @return string
     */
    public function getHref()
    {
        return $this->getUrl('custom/controller/action');
    }

    /**
     * @return \Magento\Framework\Phrase
     */
    public function getLabel()
    {
        return __('Your custom link');
    }
}

Then you need also to create the template file we defined in the block class and add the following code

<li class="link custom">
    <a <?php /* @escapeNotVerified */ echo $block->getLinkAttributes() ?>><?php echo $block->escapeHtml($block->getLabel()) ?></a>
</li>
  • Hi Nikolas, Thanks for the reply but want to achieve dropdown with customer name or Email ID when customer is logged in. Before that only Sign In/Up Label will show up in top links. Then i need to show all the links{My Account, My Orders, Custom Link, Sign Out) in dropdown as shown in screenshot. – Vinay Oct 22 '17 at 4:31
0

First create default.xml in your module (I am assuming that you already know how to create module in magento2) with below path

app/code/YOURNAME_SPACE/YOUR_MODULE_NAME/view/frontend/layout/

and paste below code

<?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="top.links">
            <block class="YOUR_NAMESPACE\YOUR_MODUE_NAME\Block\TopLink" name="your-link-top">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">Custom Link</argument>
                    <argument name="path" xsi:type="string" translate="true">custom-path</argument>                    
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Now create Block TopLink.php in your module with below path,

YOUR_NAMESPACE\YOUR_MODUE_NAME\Block

<?php

namespace YOUR_NAMESPACE\YOUR_MODUE_NAME\Block;

use Magento\Customer\Model\Context;
use Magento\Framework\View\Element\Template\Context as TemplateContex;
use Magento\Customer\Model\Session;

class TopLink extends \Magento\Framework\View\Element\Html\Link {


    /**
     *
     * @var Session
     */
    protected $session;

    /**
     * 
     * @param TemplateContex $context    
     * @param Session $session
     * @param array $data
     */
    public function __construct(
    TemplateContex $context, Session $session,array $data = []) {        
        $this->session = $session;
        parent::__construct($context, $data);
    }

    /**
     * Render block HTML.
     *
     * @return string
     */
    protected function _toHtml() {
        if (false != $this->getTemplate()) {
            return parent::_toHtml();
        }        
        if ($this->session->isLoggedIn()) {
            return '<li><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLinkLabel()) . '</a></li>';
        }
    }

    /**
     * 
     * @return string
     */
    private function getLinkLabel() {
        return 'Custom Link';
    }    

}
  • Hi Keyur, I have created the same module as shown above but no custom Link is showing up in the TopLinks. – Vinay Oct 22 '17 at 10:14
  • You are login into site because as per the comment you need this link after login. Also please clear your cache @Vinay – Keyur Shah Oct 22 '17 at 10:15
  • Hi Keyur, the link is showing up when customer will login. I want the Sign In/Up Link in Top Links, When customer Click on Sign In/Up and log in with that, then on the same place Customer Name Or Email Id will Appear with the DROPDOWN having like "My Account" "My Orders" and "Sign Out" – Vinay Oct 22 '17 at 10:22
  • the code does the same thing – Keyur Shah Oct 22 '17 at 10:24
  • Hi Keyur Rechecked Twice, A Custom Link is coming Only when customer is Logged in. How to show Sign In/Up and after login same link(Sign In/Up) replaces with Customer Name or Customer Email ID. No Dropdown is not coming on hover with "Custom Links". – Vinay Oct 22 '17 at 10:31

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.