2

I've added my custom link to top.links using layout.xml

Vendor/Module/view/frontend/layout/default.xml

<?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="Magento\Framework\View\Element\Html\Link\Current" name="something-link">
            <arguments>
                <argument name="label" xsi:type="string">Create Something</argument>
                <argument name="path" xsi:type="string">Route/Controller/index</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

One More I've added at Customer Account Page

Vendor/Module/view/frontend/layout/customer_account.xml

<?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="customer_account_navigation">
        <block class="Magento\Framework\View\Element\Html\Link\Current" name="something-name">
            <arguments>
                <argument name="path" xsi:type="string">route/controller/index</argument>
                <argument name="label" xsi:type="string">My Something</argument>
                <argument name="sortOrder" xsi:type="number">2</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

  1. First Link which is at top.links I would like to only show if customer is logged in.
    1. Second will only display if customer is in Customer Group That is Wholesale.

How can i achieve this ???

Any Ideas ??

0
3

For the top.links and customer_account_navigation you can create your Block class Instead of Magento\Framework\View\Element\Html\Link\Current and add your condition there, like

<?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="YOURNAMESPACE\YOURMODULE\Block\Link" name="something-link">
            <arguments>
                <argument name="label" xsi:type="string">Create Something</argument>
                <argument name="path" xsi:type="string">Route/Controller/index</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

Now you need to create your own block class,

YOURNAMESPACE\YOURMODULE\Block\Link which should extends extends \Magento\Framework\View\Element\Template

and in this class you can add your condition in _toHtml() function for example,

protected function _toHtml()
    {
        if (something) { // here you can add your custom condition
            return parent::_toHtml();
        }
        return '';
    }

Example,

<?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="StackExchange\Test\Block\Link" name="something-link">
            <arguments>
                <argument name="label" xsi:type="string">Create Something</argument>
                <argument name="path" xsi:type="string">Route/Controller/index</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

Block file,

<?php

namespace StackExchange\Test\Block;

use Magento\Customer\Model\Context;
use Magento\Customer\Block\Account\SortLinkInterface;


class Link extends \Magento\Framework\View\Element\Html\Link implements SortLinkInterface
{

    /**
     * Render block HTML
     *
     * @return string
     */
    protected function _toHtml()
    {
        if (check login condition) { // replace with your condition
            return parent::_toHtml();
        }
        return '';
    }

    /**
     * {@inheritdoc}
     * @since 100.2.0
     */
    public function getSortOrder()
    {
        return $this->getData(self::SORT_ORDER);
    }
}
2
  • I solved my problem, There's one little help needed, I would like to add css_class to new link at account side bar, I added using ` <argument name="css_class" xsi:type="string">nav item</argument>` Dont know why its not working, I checked vendor they used same as i did. Any ideas ?!! – anonymous May 3 '18 at 6:40
  • how to get customer session class in __construct because its throwing error when we used __construct public function __construct( \Magento\Customer\Model\Session $customerSession ) { $this->customerSession = $customerSession; } – Manish Maheshwari Dec 3 '18 at 5:36

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.