0

I have layout file have block like this

<referenceContainer name="sidebar.additional">
   <block class="Partsoto\CategoryCustom\Block\Search" name="category.custom.replacement.search" before="-"  template="Partsoto_CategoryCustom::replacement_sidebar.phtml"/>
</referenceContainer>

I want to set condition if querystring = "replacement?code=AAA"

show container name = "sidebar.additional" .

Is it possible?

  • Can please put your condition properly, it the condition is ?code=AAA – Amit Bera Jun 15 '18 at 3:03
  • only donot want to show container name sidebar.additional? – Amit Bera Jun 15 '18 at 3:08
  • This is page for show brand . only 'AAA' brand show container name sidebar.additional . rest of brands page don't show this sidebar. – stevieki Jun 15 '18 at 3:12
0

You have to create a module/extension for your requirement.

In magento2, the best way to remove a container that via layout

<referenceContainer name="{Containername}" remove="true"/>

So, lets create a handle which will create a layout handler like default.xml catalog_product_view.xml,catalog_product_view.xml etc using event layout_load_before (Event/Observer).

Using that custom handle files remove the sidebar.additional

In your case, create a handle which will only create when you have a query ?code=AAA and the query param code is not equal AAA.

Create events.xml at app/code/{VendorName}/{ModuleName}/etc/frontend/

and code like:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="renerate_custom_handle" instance="{VendorName}\{ModuleName}\Observer\CreateNewHandler" />
    </event>
</config>

Observer file CreateNewHandler.php should be located at app/code/{VendorName}/{ModuleName}/Observer

   <?php
namespace {VendorName}\{ModuleName}\Observer;


use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class CreateNewHandler implements  ObserverInterface
{

    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $layout = $observer->getLayout();



       $lets= (isset($_GET['code'])&& $_GET['code'] !='AAA' && strlen($_GET['code'])> 0 )
        ?'sidebar_remove_brand': null ;
        if($lets != null){
            $layout->getUpdate()->addHandle($lets);
        }

        return $this;
    }
}

Last create a layout file sidebar_remove_brand.xml at app/code/{VendorName}/{ModuleName}/view/frontend/layout

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <!--
    <referenceContainer name="footer-container" remove="true"/>
    -->
    <referenceContainer name="sidebar.additional" remove="false" />
</body>
</page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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