Ich muss alle untergeordneten Blöcke im Controller unter Verwendung des übergeordneten Blocknamens oder des Layouts Magento 2 blockieren.
Danke im Voraus.
Ich muss alle untergeordneten Blöcke im Controller unter Verwendung des übergeordneten Blocknamens oder des Layouts Magento 2 blockieren.
Danke im Voraus.
Antworten:
Stellen Sie sicher, dass Ihr Controller identisch ist, wodurch das Layout geladen wird, in dem Ihr Block und seine untergeordneten Blöcke beendet werden.
Verwenden Sie Magento \ Backend \ App \ Action \ Context.
Verwenden Sie Magento \ Framework \ View \ Result \ PageFactory.
Klasse Bearbeiten erweitert \ Magento \ Backend \ App \ Action
{
protected $ resultPageFactory;
öffentliche Funktion __construct (
Kontext $ Kontext,
PageFactory $ resultPageFactory
) {
$ this-> resultPageFactory = $ resultPageFactory;
parent :: __ Konstrukt ($ context);
}}
$ resultPage = $ this-> resultPageFactory-> create ();
$ blockInstance = $ resultPage-> getLayout () -> getBlock ('your.block.name');
$ childBlocks = $ blockInstance-> getChildNames ();
foreach ($ childBlocks als $ blockName) {
$ block = $ resultPage-> getLayout () -> getBlock ($ blockName);
}}
}}
$blockInstance->getChildNames();hier bedeutet getChildNames ()?
Sie können alle untergeordneten Blöcke wie folgt erhalten.
$allblocks = $block->getChildNames();
foreach($allblocks as $blocks){
$child_block = $this->resultPageFactory->create()->getLayout()->getBlock($blocks);
}