0

I would like to duplicate the filters block content on layered category pages. I'm using this code :

<referenceContainer name="category.product.list.additional">
    <action method="append">
        <argument name="block" xsi:type="string">catalog.leftnav</argument>
        <argument name="as" xsi:type="string">alias.name</argument>
    </action>
</referenceContainer>

but instead of duplicate the block it moves it to the new container and remove it from the left sidebar.

Thanks !

1
  • Has this bounty not been accepted? Commented Jun 7, 2020 at 7:02

2 Answers 2

2

Using an additional block (whose class is Mbs\ProductList\Block\LeftNavClone in my example below) that instantiates the block catalog.leftnav like below does the effect you seem to aim for:

<referenceContainer name="category.product.list.additional">
    <block class="Mbs\ProductList\Block\LeftNavClone" name="catalog.leftnav.clone">
        <arguments>
            <argument name="originalBlock" xsi:type="string">catalog.leftnav</argument>
        </arguments>
    </block>
</referenceContainer>

the code for the block as below:

<?php
    namespace Mbs\ProductList\Block;
        
    class LeftNavClone extends \Magento\Framework\View\Element\Template
    {
        protected function _toHtml()
        {
            $clone = $this->getLayout()->getBlock($this->getData('originalBlock'));
            if ($clone) {
               return $clone->toHtml();
            }
        }
    }
1
  • This php code was unable to set different phtml file using setTemplateMethod. So i've created my own. you can use below. $clone = $this->getLayout()->getBlock($this->getData('originalBlock')); $orgTemplate = $clone->getTemplate(); if ($clone) { $clone->setTemplate('Magento_Theme::html/collapsible-mobile.phtml'); $html = $clone->toHtml(); $clone->setTemplate($orgTemplate); return $html; } return ''; It's working fine for me. Commented Nov 5, 2023 at 13:57
0

I added a block to my theme by cloning the block class and adding the before and after tags to make one sit on top of the other. I needed different CCS for mine so I created some new CSS classes. In mine I wanted the new block to sit directly above the old one. See code below.

Note the first block has the before="-" tag and the second block has the after="-" tag.

<referenceContainer name="header.container">
    <container name="rd.navbar-wrap" htmlTag="div" htmlClass="rd-navbar">
        <container name="rd.navbar" htmlTag="div" htmlClass="rd-navbar-panel layout_1">
            <block class="Magento\Cms\Block\Block" name="header_info_2" before="-">
                <arguments>
                    <argument name="block_id" xsi:type="string">header_info_2</argument>
                </arguments>
            </block>
            <block class="Magento\Cms\Block\Block" name="header_info" after="-">
                <arguments>
<argument name="block_id" xsi:type="string">header_info</argument>
                </arguments>
            </block>
        </container>
    </container>
</referenceContainer>

Once I added these changes to layout.xml in my theme I created a new block in the admin panel with the block identifier "header_info_2" and it showed up correctly. There was quite a few CSS files with the original class header-info so I had to create new custom classes that corresponded to the new blocks design.

The original code block in layout.xml looked like this:

<referenceContainer name="header.container">
    <container name="rd.navbar-wrap" htmlTag="div" htmlClass="rd-navbar">
        <container name="rd.navbar" htmlTag="div" htmlClass="rd-navbar-panel layout_1">
            <block class="Magento\Cms\Block\Block" name="header_info" before="-">
                <arguments>
                    <argument name="block_id" xsi:type="string">header_info</argument>
                </arguments>
            </block>
        </container>
    </container>
</referenceContainer>

Hope this helps

Below is an image of the 2 blocks on the frontend homepage. The top one is the new one with the new CSS.

New block in Magento 2.3.4 homepage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.