调用$ layout-> getChildBlocks('customer_account_navigation')在Magento 2中返回空数组

时间:2019-12-26 10:35:36

标签: magento magento2

我写了下面的类,因为我想在登录后加载客户可用的customer_account_navigation块。我希望此块显示在主页(cms_index_index)上。 我知道可以将<update handle="customer_account" />添加到布局xml指令中。我确实没有运气。对于下面编写的类,我希望只调用$layout->getChildBlocks('customer_account_navigation')才能检索所有可用的链接,但是当我调用getChildBlocks('customer_account_navigation')时,会得到一个空数组。只是想知道我在做什么错。

<?php
namespace Vendor\CustomerNavigationHomepageWidget\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\View\LayoutInterfaceFactory;
use Magento\Customer\Block\Account\Navigation;

class Sidebar implements ArgumentInterface{

private $_layoutFactory;

private $_navigation;

public function __construct(
    LayoutInterfaceFactory $layoutFactory,
    Navigation $navigation
)
{
    $this->_layoutFactory = $layoutFactory;
    $this->_navigation = $navigation;
}

public function getMenu(){

    $layout = $this->_layoutFactory->create();
    $layout->getUpdate()->addHandle('customer_account');
    $layout = $this->loadLayout($layout);
    $layout->addBlock(Navigation::class, 'customer_account_navigation');

}

/**
 * @param $layout
 * @return mixed
 */
public function loadLayout($layout)
{
    $layout->getUpdate()->load();
    $layout->generateXml();
    $layout->generateElements();
    return $layout;
}

}

0 个答案:

没有答案