Overriding Magento\Catalog\Model\Layer\FilterList in Magento 2.2.3
If you’re trying to override the Magento\Catalog\Model\Layer\FilterList in Magento 2.2.3 using a plugin, you might encounter a fatal error due to type mismatch. In this guide, we’ll walk through how to properly override this model while avoiding the TypeError issue.
Understanding the Error
The error message indicates that the second argument passed to your plugin’s aroundGetFilters() method is expected to implement the Magento\Catalog\Model\Layer\FilterableAttributeListInterface, but a Closure instance was given instead. This occurs because Magento 2 uses proxies and interceptors, which can cause mismatches in interface implementations.
The Incorrect Approach: Using Preference
Many tutorials suggest using preferences to override core models. However, as you mentioned, preferences don’t work correctly for the FilterList model due to its complex architecture involving plugins and interceptor chains.
The Correct Solution: Using a Proper Plugin
To properly override the getFilters() method of the FilterList model in Magento 2.2.3, we’ll create a plugin that adheres to the correct interface requirements. Here’s how:
-
Create your custom module:
First, create a new module using your preferred development tool (e.g., Composer or Magento Setup). For this example, let’s assume you’ve created
vendor_moduleunderapp/code/Vendor/Module. -
Override the getFilters() method:
In your module, navigate to
etc/frontend/di.xml. Add the following plugin configuration:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Layer\FilterList">
<plugin name="vendor_module_overrides_filterlist" type="Vendor\Module\Model\Plugin\FilterList" sortOrder="10"/>
</type>
</config>
-
Create the plugin class:
In your module’s
Modelfolder, create a new file calledPlugin.php. Implement thearoundGetFilters()method to override the core logic:
namespace Vendor\Module\Model\Plugin;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\Layer\FilterList as Subject;
use Magento\Catalog\Model\Layer\FilterableAttributeList;
class FilterList
{
public function aroundGetFilters(Subject $subject, \Closure $proceed)
{
// Your custom logic here
return $proceed();
}
}
-
Implement the required interface:
In your
Plugin.phpfile, make sure the class implements theMagento\Catalog\Model\Layer\FilterListInterface. This ensures that the plugin can correctly intercept and modify thegetFilters()method.
namespace Vendor\Module\Model\Plugin;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\Layer\FilterList as Subject;
use Magento\Catalog\Model\Layer\FilterableAttributeList;
class FilterList implements \Magento\Catalog\Model\Layer\FilterableAttributeListInterface
{
// ... (same code as above)
}
Conclusion
Overriding the Magento\Catalog\Model\Layer\FilterList in Magento 2.2.3 requires a deep understanding of the core model’s structure and how plugins interact with its methods. By using a properly configured plugin that implements the necessary interfaces, you can successfully override the getFilters() method without encountering type errors.
Remember to clear your cache after making these changes:
php bin/magento cache:flush
For more advanced Magento 2 development topics and tutorials, visit our blog on LaravelCompany.com.