Magento 2 – VirtualType Argument Replacement

magento2overridesvirtualtype

I'm trying to remove the category filter in category view. But apart from that, my question is more about Magento 2 itself.

in di.xml of Magento_CatalogSearch , you just have to comment the "category" item to do that :

<virtualType name="categoryFilterList" type="Magento\Catalog\Model\Layer\FilterList">
    <arguments>
        <argument name="filters" xsi:type="array">
            <item name="attribute" xsi:type="string">Magento\CatalogSearch\Model\Layer\Filter\Attribute</item>
            <item name="price" xsi:type="string">Magento\CatalogSearch\Model\Layer\Filter\Price</item>
            <item name="decimal" xsi:type="string">Magento\CatalogSearch\Model\Layer\Filter\Decimal</item>
            <item name="category" xsi:type="string">Magento\CatalogSearch\Model\Layer\Filter\Category</item>
        </argument>
    </arguments>
</virtualType>

So I was trying to remove this argument, in a custom module, just copy/pasting it into my di.xml and removing the item category. But it does not work.

And yes, I wrote well my module.xml, with the sequence and depends nodes.

Is this possible, or do i have to do another Virtual Type? This way I could write a preference for categoryFilterList, but is this possible to modify an existing argument ?

Best Answer

the di.xml files are merged, so it if you copy the markup you mentioned in your module and remove a line, when the merge is done, the original file will be merge too and you will still get that line.

An option would be to create your own virtual type that does not contain the category item and make all the models that used the original catalogFilerList virtual type, use your own.
Something like this:

<virtualType name="MyCustomCategoryFilterList" type="Magento\Catalog\Model\Layer\FilterList">
    <arguments>
        <argument name="filters" xsi:type="array">
            <item name="attribute" xsi:type="string">Magento\CatalogSearch\Model\Layer\Filter\Attribute</item>
            <item name="price" xsi:type="string">Magento\CatalogSearch\Model\Layer\Filter\Price</item>
            <item name="decimal" xsi:type="string">Magento\CatalogSearch\Model\Layer\Filter\Decimal</item>
            <item name="category" xsi:type="string">Magento\CatalogSearch\Model\Layer\Filter\Category</item>
        </argument>
    </arguments>
</virtualType>

Then in the frontend/di.xml of your module add this:

 <virtualType name="Magento\LayeredNavigation\Block\Navigation\Category" type="Magento\LayeredNavigation\Block\Navigation">
    <arguments>
        <argument name="filterList" xsi:type="object">MyCustomCategoryFilterList</argument>
    </arguments>
</virtualType>

That's the only one I found that uses categoryFilterList.

Related Topic