分类
Magento PHP

magento2 会员中心 customer_account.xml 菜单程序控制是否显示

customer_account.xml
view/frontend/layout/customer_account.xml
<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="Maijindou\Customer\Model\Current" name="customer-account-invoices-link">
    <arguments>
        <argument name="path" xsi:type="string">maijindousales/invoice/index</argument>
        <argument name="label" xsi:type="string" translate="true">Invoices</argument>
        <argument name="sortOrder" xsi:type="number">190</argument>
    </arguments>
</block>
</referenceBlock>
    </body>
</page>
Maijindou\Customer\Model\Current
<?php

namespace Maijindou\Customer\Model;
use Magento\Customer\Block\Account\SortLinkInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\DefaultPathInterface;
use Magento\Framework\View\Element\Template\Context;

class Current extends \Magento\Framework\View\Element\Html\Link\Current implements SortLinkInterface
{
    private $session;
    private $public_path = [
        'customer/account/edit',
        'maijindousales/buyer',
        'customer/address',
        'vendorattribute/account/customfields',
        'helpdesk/ticket',
        'message'
    ];

    public function __construct(
        Context $context,
        Session $session,
        DefaultPathInterface $defaultPath,
        array $data = [])
    {
        $this->session = $session;
        parent::__construct($context, $defaultPath, $data);
    }
    public function _toHtml()
    {
        $wkv_mjd_company_approved = $this->session->getCustomer()->getData('wkv_mjd_company_approved');
        if ($wkv_mjd_company_approved == 'approved') {
            return parent::_toHtml();//全部显示
        } else {
            if(in_array($this->getPath(),$this->public_path)){
                return parent::_toHtml();//显示的部分URL
            }else{
                return '';//不显示
            }
        }
    }
    /**
     * {@inheritdoc}
     */
    public function getSortOrder()
    {
        return $this->getData(self::SORT_ORDER);
    }
}