2

I have created a custom menu on customer account. I want to show the menu only when a specific customer attribute value is set. I am not able to find out whether there is any way to show the menu conditionally in Magento2?

To show the custom menu, I am using this in view/layout/frontend/customer_account.xml

<referenceBlock name="customer_account_navigation">
    <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-customfield-index-link">
        <arguments>
            <argument name="path" xsi:type="string">mymodule/customfields</argument>
            <argument name="label" xsi:type="string" translate="true">Identity Proof</argument>
        </arguments>
    </block>
</referenceBlock>
6

First You need create block extents class Magento\Framework\View\Element\Html\Link\Current

After that use use it in to your template view/layout/frontend/customer_account.xml like this

<referenceBlock name="customer_account_navigation">
    <block class="Namespace\Module\Block\View\Element\Html\Link\Current" name="customer-account-customfield-index-link">
        <arguments>
            <argument name="path" xsi:type="string">mymodule/customfields</argument>
            <argument name="label" xsi:type="string" translate="true">Identity Proof</argument>
        </arguments>
    </block>
</referenceBlock>

Then overwrite function toHtml() like this

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

Which full block code

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,
        CustomerSession $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 '';
    }  
}
  • This worked but I had to fix the error "Uncaught TypeError: Argument 2 passed to Magento\Framework\View\Element\Html\Link\Current::__construct() must implement interface Magento\Framework\App\DefaultPathInterface, array given, called in" Did this by changing parent::__construct($context, $data); to parent::__construct($context, $defaultPath); – tim.baker Jul 21 '17 at 14:17
  • just run setup:di:compile :) – HoangHieu Jul 21 '17 at 14:19
  • When I do that I get Incompatible argument type: Required type: \Magento\Framework\App\DefaultPathInterface. Actual type: array; File: F:/dev/core/app/code/Company/Module/Block/MagentoExtends/View/Element/Html/Link/CurrentLoggedOut.php though :( – tim.baker Jul 21 '17 at 14:41
  • please remove var/di var/generation. and run compile again, make sure namespace correct. – HoangHieu Jul 21 '17 at 15:08
  • parent::__construct($context, $data); to parent::__construct($context, $defaultPath); I saw you have changed that. Please parent::__construct($context, $data); is default magento contructor don't change this. – HoangHieu Jul 21 '17 at 15:09

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.