Question

I have created a custom block, as I want to add this layout to my current layout.

Following is my block:

<referenceContainer name="sidebar.main">
        <block class="Vendor\ModuleName\Block\Frontend\Block" name="ucTntlZsQVjVytW4xjtfdypv9LTzRsFP" template="Vendor_ModuleName::myBlock.phtml"/>
    </referenceContainer>

I want to add this block to my current block for that I created following event:

core_layout_render_element

Now how can I add this block to layout using PHP

Need Help!!

Was it helpful?

Solution

Try this event: layout_load_before

<event name="layout_load_before">
    <observer name="bannerlayoutloadbefore" instance="Vendor\ModuleName\Observer\Bannerlayoutloadbefore" shared="false" />
</event>

And use the below php code to render the layout:

$layout = '<referenceContainer name="sidebar.main">
        <block class="Vendor\ModuleName\Block\Frontend\Block" name="ucTntlZsQVjVytW4xjtfdypv9LTzRsFP" template="Vendor_ModuleName::myBlock.phtml"/>
    </referenceContainer>';
$observer->getLayout()->getUpdate()->addUpdate($layout);

Use the above code in your observer's execute function.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top