1

Hi guys i am trying to hide few links under the customer navigation panel conditionally.

i have these setup done.

customer_account.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="customer-account-create-agent-createagent">
            <arguments>
               <argument name="path" xsi:type="string">my/agent/createagent</argument>
               <argument name="label" xsi:type="string">Create Agent</argument>
            </arguments>
        </block>
        <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-create-agent-viewagent">
            <arguments>
               <argument name="path" xsi:type="string">my/agent/viewagent</argument>
               <argument name="label" xsi:type="string">View Agent</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>
</page>

current.php

<?php

namespace [vendorname]\[module]\Block\Agent;
// namespace Namespace\Module\Block\View\Element\Html\Link;

//use Magento\Customer\Model\Session as CustomerSession;

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

protected $_customerSession;
/**
 * Constructor
 *
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Framework\App\DefaultPathInterface $defaultPath
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\App\DefaultPathInterface $defaultPath,
    \Magento\Customer\Model\Session $CustomerSession,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->_defaultPath = $defaultPath;
    $this->_customerSession = $CustomerSession;
}


public function toHtml(){

      if($this->_customerSession->getCustomer()->getYourAttribute()){
           return parent::toHtml();
      }
      return '';
   }  
}

I wanted to hide the two links named "Agent Create" and "Agent View" id the user is not of a particular group.

This is an implementaion i was trying , but i didnt get any result. Please help !

5

You can do it using an observer.

Create following files in your custom module.

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_generate_blocks_after">
        <observer name="removeblock" instance="Vendor\Module\Observer\Removeblock" shared="false" />
    </event>
</config>

app/code/Vendor/Module/Observer/Removeblock.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class Removeblock implements \Magento\Framework\Event\ObserverInterface
{

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $layout = $observer->getLayout();

        $your_condition = 1; // write your conditional logic here

        if($your_condition == 1){
            $layout->unsetElement('your_block_name');
        }
    }
}
0
0

You can try following:

<?php


namespace Vendor\ModuleName\Block;

use Magento\Framework\View\Element\Html\Link\Current;
use \Magento\Framework\View\Element\Template\Context;
use \Magento\Framework\App\DefaultPathInterface;

class BlockName extends Current
{

    /**
     * BlockName constructor.
     * @param Context $context
     * @param DefaultPathInterface $defaultPath
     */
    public function __construct(
        Context $context,
        DefaultPathInterface $defaultPath
        ) 
        {
        parent::__construct($context, $defaultPath, []);
    }

    public function manageLink()
    {
        if (condition) {
            $this->getLayout()->unsetElement('blockname');
        }
    }
}

create file : app/code/Vendor/ModuleName/view/frontend/layout/customer_account.xml

<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="Vendor\ModuleName\Block\Blockname" name="blockname">
                <arguments>
                    <argument name="path" xsi:type="string">frontname/controller/action</argument>
                    <argument name="label" xsi:type="string">name of label</argument>
                </arguments>
                <action method="manageLink"></action>
            </block>
        </referenceBlock>
    </body>
</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.