L
L
lagudal2021-12-02 15:31:54
Magento
lagudal, 2021-12-02 15:31:54

How to add container to layout in M2 for only 2columms-left pages?

I can’t understand, it is necessary to add a container to 2-column pages in a custom module, more specifically, add divs something like another sidebar, inside colunms, at the same level as sidebar main and sidebar additional.
It seems that everything should be simple, in the layout folder of the module - vendor/my_vendor/mymodule/view/frontend/layout I create default.xml , in which I create the container I need

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="columns">
            <container name="div.sidebar.alt" htmlTag="div" htmlClass="sidebar sidebar-main-alt" after="div.sidebar.main">
                <container name="sidebar.alt" as="sidebar_alt" label="Sidebar Main Alternative">
                    <block>
                    ...тут содержимое блока
                    </block>
                </container>
            </container>
        </referenceContainer>
    </body>
</page>

However, despite the fact that page has layout="2columns-left" , the newly created container is visible not only on pages with 2 columns, but also with one. Those. on pages with 2 columns, columns has main, sidebar-main, sidebar-additional and my custom sidebar-alt. And on a single-column page, sidebar-main and sidebar-additional are missing, as expected, and my custom sidebar-alt remains and goes right after main.
We need this container to be visible only on pages with 2 columns, but at the same time on any pages that have a 2columns-leflt layout.
Moreover, if you overwrite in the forehead in vendor/magento/module-theme/view/frontend/page_layout/2columns-left.xml, add this container right there, then everything works as it should.
I tried to overwrite 2columns-left, created a page_layout folder in the frontend folder of my module and overridden 2columns-left.xml there, but no, it doesn’t work that way.
How to do what I need?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question