0

I would like to hide link in user account customer_account.xml based on role they are in. Let say we have group B2B and i want to hide block customer-account-navigation-product-reviews-link.

To remove this block I need flowing code: <referenceBlock name="customer-account-navigation-product-reviews-link" remove="true"/>

To remove it in condition i know that I will require ifconfig <referenceBlock name="customer-account-navigation-product-reviews-link" ifconfig="??" remove="true"/>

However i have no idea how to get that true/false condition here. Is this require special class if so were to put it and how it might look ?

1
  • Is any solution worked for you? – Rahul Barot May 28 at 4:31
1

In order to conditionally remove block from layout use Magento 2 event layout_generate_blocks_after as below.

Define the event in events.xml file.

<?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 instance="Vendor\YourModule\Observer\Frontend\Removeblocks" name="layout_generate_blocks_after"/>
    </event>
</config>

Define Observer file Removeblocks.php and add below code in file.

<?php

namespace Vendor\YourModule\Observer\Frontend;
 
use Magento\Framework\Event\ObserverInterface;
 
class Removeblocks implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       //your condition here
       $layout = $observer->getLayout();
       $layout->unsetElement('customer-account-navigation-product-reviews-link');
    }
}

I hope it helps you. Thanks!

1
  • execute should be (\Magento\Framework\Event\Observer $observer) – user2217288 May 31 at 9:03
0

I don't think you'll be able to pull in values like that within the XML as it will be on a per-customer basis and the ifconfig values are static.

Your best bet will probably be to write a plugin for the _toHtml() function in

vendor/magento/framework/View/Element/Html/Link/Current.php

where the individual links are rendered.

You could then use the Magento\Customer\Model\Session class to get the customer group ID ($customerSession->getCustomer()->getGroupId();) which you can check before rendering the unwanted item.

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.