我必须使用父块名称或布局Magento 2获取控制器中的所有子块。
提前致谢。
我必须使用父块名称或布局Magento 2获取控制器中的所有子块。
提前致谢。
Answers:
确保加载加载块及其子块退出的布局的控制器相同。
使用Magento \ Backend \ App \ Action \ Context;
使用Magento \ Framework \ View \ Result \ PageFactory;
类Edit扩展\ Magento \ Backend \ App \ Action
{
受保护的$ resultPageFactory;
公共功能__construct(
上下文$ context,
PageFactory $ resultPageFactory
){
$ this-> resultPageFactory = $ resultPageFactory;
父母:: __ construct($ context);
}
$ resultPage = $ this-> resultPageFactory-> create();
$ blockInstance = $ resultPage-> getLayout()-> getBlock('your.block.name');
$ childBlocks = $ blockInstance-> getChildNames();
foreach($ childBlocks as $ blockName){
$ block = $ resultPage-> getLayout()-> getBlock($ blockName);
}
}
$blockInstance->getChildNames();这里的getChildNames()是什么意思?
您可以像这样获得所有子块。
$allblocks = $block->getChildNames();
foreach($allblocks as $blocks){
$child_block = $this->resultPageFactory->create()->getLayout()->getBlock($blocks);
}