Magento – Magento 2 Add block to custom layout via admin panel widget

admincontainerslayoutmagento2widget

I've made a custom theme and custom homepage layout that renders a custom container and inside it a block with a custom template:

custom_homepage.xml inside Magento_Theme/page_layout folder

    <?xml version="1.0"?>
    <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
        <update handle="empty" />
        <referenceContainer name="page.wrapper">
            <!-- <container name="header.container" as="header_container" label="Page Header Container"  htmlTag="header" htmlClass="page-header" before="main.content"/> -->
            <container name="page.top" as="page_top" label="After Page Header" after="header.container"/>
            <container name="footer-container" as="footer" before="before.body.end" label="Page Footer Container" htmlTag="footer" htmlClass="page-footer" />


           <!-- MY CUSTOM CONTAINER AND BLOCK WITH CUSTOM TEMPLATE -->
            <container name="custom.container" as="customContainer" label="Custom Container" htmlTag="div" htmlClass="custom-container">
                <block class="Magento\Framework\View\Element\Template" name="testing" type="cms/block" template="Magento_Theme::test.phtml" />
            </container>
        </referenceContainer>
    </layout>

I also have a layouts.xml which is:

<?xml version="1.0" encoding="UTF-8"?>
<page_layouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:noNamespaceSchemaLocation="urn:magento:framework:View/PageLayout/etc/layouts.xsd">

    <layout id="custom_homepage">
        <label translate="true">Custom Homepage</label>
    </layout>
</page_layouts>

So in my admin section, I can go to content -> pages -> and set this layout as a custom homepage. Everything renders and I get the text from test. phtml inside Magento_Theme/templates which is:

<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatem suscipit, adipisci, cum quos, alias similique ad eum at deserunt eligendi enim dignissimos, unde vero ipsam voluptatibus cumque accusantium! Obcaecati, quasi.</h2>

Now I want to go and add another block to my custom container from above as a widget from the admin panel. So I go to admin/content/widget/new widget and under 'Add Layout Update' I select Page Layout and under Page tab, I do not see my custom homepage here, neither 1 column, 2 columns with right bar and other made by default.

Also under the container tab, I do not see my label (Custom Container), only the default ones like (Page Footer Container, After Page Header, etc..)

What am I doing wrong?

Thanks!

Best Answer

add your content to default.xml then select in your widget config: Display on * Specified Page

and your container should be visible in the dropdown

Related Topic