1

I am developing a module so that for categories that have subcategories the subcategories are listed instead of the products.

My catalog_category_view_type_layered.xml file looks like this

<?xml version="1.0"?>
<body>
    <referenceBlock name="category.products" remove="true"/>
    <referenceContainer name="content">
        <block class="MyName\MyModule\Block\Category\View" name="category.categories" template="MyName_MyModule::category/categories.phtml" />
    </referenceContainer>
</body>

but I need some logic (I imagine with PHP) to only apply these layout changes conditionally.

1 Answer 1

1

You can put IF condition in Layout XML file of Magento to show or hide any block. The IF condition checks for the Yes/No value of configuration settings. Suppose, you have a custom module. You have a custom block that you want to show in left and right sidebar.

Suppose, you have a custom module. You have a custom block that you want to show in left and right sidebar. You have two Yes/No configuration settings to show/hide the custom block on left sidebar and right sidebar.

Then, you can first define your custom block and then use IF condition (ifconfig) in the action method append to show or hide the block in left or right sidebar. If the configuration settings for left sidebar is set as ‘Yes‘ then the block will be displayed in left sidebar. If it is set as ‘No‘ then the block will not be displayed in the left sidebar. It’s similar for right sidebar as well.

<default>
<block type="yourmodule/yourmodule" name="yourmodule.sidebar" before="-" template="yourmodule/block_sidebar.phtml"/>
<reference name="left">      
    <action method="append" ifconfig="yoursection/yourgroup/yourfield_to_show_hide_left_sidebar">
        <block>yourmodule.sidebar</block>
    </action>
</reference>        
<reference name="right">
    <action method="append" ifconfig="yoursection/yourgroup/yourfield_to_show_hide_right_sidebar">  
        <block>yourmodule.sidebar</block>
    </action>
</reference>
</default>

you can check this on my blog post.

4
  • It's more complicated than a yes/no config value as the condition is if the category has subcategories or not. Can the config value somehow be dynamic and check the category before returning a yes or no?
    – Alex
    May 10, 2016 at 8:44
  • To best of my knowledge IF/Else only return yes or no. I believe best approach will be to calculate the name parameter using a helper method. ` <name helper="helpername/helpermethod"/>` Then you can write your logic in the helper method. May 10, 2016 at 8:52
  • Have you got any examples of this being done? I've never heard of helper classes being used in xml layouts before
    – Alex
    May 10, 2016 at 9:12
  • check this out link I dont have practical working example but this link will give you an idea May 10, 2016 at 10:07

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.