Devo ottenere tutti i bambini bloccati nel controller usando il nome del blocco genitore o il layout Magento 2.
Grazie in anticipo.
Devo ottenere tutti i bambini bloccati nel controller usando il nome del blocco genitore o il layout Magento 2.
Grazie in anticipo.
Risposte:
Assicurati che il tuo controller sia lo stesso che carica il layout in cui esce il tuo blocco e i suoi blocchi figlio.
usa Magento \ Backend \ App \ Action \ Context;
usa Magento \ Framework \ View \ Result \ PageFactory;
class Edit estende \ Magento \ Backend \ App \ Action
{
protetto $ resultPageFactory;
funzione pubblica __construct (
Context $ context,
PageFactory $ resultPageFactory
) {
$ this-> resultPageFactory = $ resultPageFactory;
parent :: __ construct ($ contesto);
}
$ resultPage = $ this-> resultPageFactory-> create ();
$ blockInstance = $ resultPage-> getLayout () -> getBlock ('your.block.name');
$ childBlocks = $ blockInstance-> getChildNames ();
foreach ($ childBlocks come $ blockName) {
$ block = $ resultPage-> getLayout () -> getBlock ($ blockName);
}
}
$blockInstance->getChildNames();qui getChildNames () significa?
Puoi ottenere tutti i blocchi figlio in questo modo.
$allblocks = $block->getChildNames();
foreach($allblocks as $blocks){
$child_block = $this->resultPageFactory->create()->getLayout()->getBlock($blocks);
}