In Magento 1, if you wanted to programmatically add a new block to the
content block, you could do something like this
$block = $this->getLayout()->getBlock('content'); $block->append($this->getLayout()->createBlock('foo/baz'));
However, in Magento 2, this doesn't work. Calling something like
$block = $this->getLayout()->getBlock('content');
returns a value of
false into the
$block variable. I'm relatively sure this is because
content is no longer a block -- it's something Magento 2 calls a container.
Is it possible, via PHP, to get a reference to these containers and programmatically add blocks to them?
I know it's possible to do this via the new Layout DSL with something like
<body> <referenceContainer name="content"> <block ... /> </reference> </body>
What I want to know is if this is possible to do via PHP, or if PHP level access to the container blocks not possible (either by deliberate or defacto design). If it is possible -- what are the objects and methods involved?