Magento2 Layered Navigation – How to Rewrite Navigation.php

layered-navigationmagento2

i have an custom design were we overload the:
\app\design\frontend\Come\schlafgut\Magento_LayeredNavigation\templates\layer\view.phtml

We add there a new block methode .. fe. sayHello()

<?php

namespace Come\ProductListSwatches\Block\Catalog;

class Swatches extends \Magento\LayeredNavigation\Block\Navigation
{
    public function sayHello()
    {
        return "something";
    }
}

In view.phtml:

<?php if ($block->canShowBlock()): ?>
    <div class="block filter">

        <?php echo $block->sayHello(); ?>

Than we create a di.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\LayeredNavigation\Block\Navigation" type="Come\ProductListSwatches\Block\Catalog\Swatches" />
</config>

We work in developer mode, but we received this error:

PHP Fatal error:  Uncaught Error: Cannot instantiate interface Magento\\Catalog\\Model\\Layer\\FilterableAttributeListInterface 
in /var/www/schlafgut/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:73\nStack trace:\n#0 /var/www/schlafgut/vendor/magento/framework/ObjectManager/ObjectManager.php(71): 
Magento\\Framework\\ObjectManager\\Factory\\Dynamic\\Developer->create('Magento\\\\Catalog...')\n#1 /var/www/schlafgut/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(126): 
Magento\\Framework\\ObjectManager\\ObjectManager->get('Magento\\\\Catalog...')\n#2 /var/www/schlafgut/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(53): 
Magento\\Framework\\ObjectManager\\Factory\\AbstractFactory->resolveArgument(Array, 'Magento\\\\Catalog...', NULL, 'filterableAttri...', 'Magento\\\\Catalog...')\n#3 /
var/www/schlafgut/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(82): Magento\\Framework\\ObjectManager\\Factory\\Dynamic\\Developer->_resolveArguments('Magento\\\\Catalog...', Array, Array)\n#4 
/var/w in /var/www/schlafgut/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php on line 73

Do anyone know what is wrong?

Best Answer

An interface which you cannot instantiate isn't had its own preference but implemented by Magento\Catalog\Model\Layer\Category\FilterableAttributeList.

It's ok to not to have a preference since FilterableAttributeListInterface isn't something general and should contain attributes for a specific case (like Category Layered Navigation, for example).

However, \Magento\Catalog\Model\Layer\FilterList depends on that interface. And Magento\LayeredNavigation\Block\Navigation is depends on \Magento\Catalog\Model\Layer\FilterList.

So, to correctly instantiate Magento\LayeredNavigation\Block\Navigation we need to pass to its constructor a configured instance of \Magento\Catalog\Model\Layer\FilterList.

Let's look where is Magento\LayeredNavigation\Block\Navigation used. We can find that it never used itself, but two virtual types use it (Magento/LayeredNavigation/etc/frontend/di.xml):

  • Magento\LayeredNavigation\Block\Navigation\Category
  • Magento\LayeredNavigation\Block\Navigation\Search

They are both customize filterList argument.

So it's all ok with your code (at least with your example), but something went wrong in place where your object should be instantiated.

Look at debugger which type do you fail to instantiate.

Related Topic