0

here is my full di.xml

 <?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Vendor\Module\Block\Catalog\Product\ProductList\Toolbar" />    

<preference for="Magento\LayeredNavigation\Block\Navigation" type="Vendor\Module\Block\CatalogSearch\Layer" />

<preference for="Magento\Catalog\Model\ResourceModel\Product\Collection" type="Vendor\Module\Model\Catalog\Resource\Product\Collection" />

</config>

In Vendor\Module\Model\Engine.php

$current_list_mode = $this->layout->createBlock('Vendor\Module\Block\Catalog\Product\ProductList\Toolbar')->getCurrentMode();

In \Vendor\Module\Block\Catalog\Product\ProductList\Toolbar.php

namespace Vendor\Module\Block\Catalog\Product\ProductList;

use Magento\Catalog\Helper\Product\ProductList;
use Magento\Catalog\Model\Product\ProductList\Toolbar as ToolbarModel;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
    protected $tglssearchHelper;

public function __construct(
    /* passing all Constructors parameters to the parent class -Magento\Catalog\Block\Product\ProductList\Toolbar.php*/
    \Magento\Framework\View\Element\Template\Context $context,
     \Magento\Catalog\Model\Session $catalogSession,
    \Magento\Catalog\Model\Config $catalogConfig,
    ToolbarModel $toolbarModel,
    \Magento\Framework\Url\EncoderInterface $urlEncoder,
    ProductList $productListHelper,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Vendor\Module\Helper\Data $tglssearchHelper,      
    \Magento\Framework\Registry $registry,
    array $data = []
)  {
        $this->tglssearchHelper = $tglssearchHelper;
        $this->_catalogSession = $catalogSession;
        $this->_catalogConfig = $catalogConfig;
        $this->_toolbarModel = $toolbarModel;
        $this->_urlEncoder = $urlEncoder;
        $this->_productListHelper = $productListHelper;
        $this->_postDataHelper = $postDataHelper;
        $this->_registry = $registry;


        parent::__construct($context,$catalogSession,$catalogConfig,$toolbarModel,$urlEncoder,$productListHelper,$postDataHelper,$data);
   }

  public function getAvailableOrders()
{
    echo 'availablOrders';

}

But still im not able to override the custom block for my custom search engine:( my layout file name : catalogsearch_catalogsearch_resultcontroller.xml

Controller path is: Vendor\Module\Controller\CatalogSearch\ResultController

My system.log says:

Cache file with merged layout: LAYOUT_frontend_STORE1_3d41d8cd98f00b204e9800998ecf8427e and handles : Please correct the XML data and try again. [] []

I get a blank page when i search for the products. please help where am i wrong!!

1 Answer 1

2

Custom block calling in di.xml Preference is like

<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Vendor\Module\Block\Catalog\Product\ProductList\Toolbar" />

I tried it is working

di.xml

<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Vendor\Module\Block\Test" />

Custom Block

namespace Vendor\Module\Block;

    class Test extends \Magento\Catalog\Block\Product\ProductList\Toolbar
    {
         public function getAvailableOrders()
         {
              echo 'test';
              exit;
         }
    }

Then ran php magento setup:upgrade, I run the product list page ...:)

Hope this helps.

9
  • Yes I tried tat.. but I get an error cause that namespace is aliased by ToolbarModel
    – Sushivam
    Jun 23, 2016 at 16:10
  • in Magento\Catalog\Block\Product\ProductList\Toolbar the namespace is given as Magento\Catalog\Block\Product\ProductList\Toolbar as ToolbarModel ... So I thot it's the alias instead of using the whole class path in di xml, I gave just "ToolbarModel" ... Pls correct me
    – Sushivam
    Jun 24, 2016 at 3:40
  • i corrected it, my error was i had given Model instead of Block but even after it, my core block is getting overriden
    – Sushivam
    Jun 24, 2016 at 6:26
  • have updated the question and also posted new question @Krishna
    – Sushivam
    Jun 24, 2016 at 7:49
  • I did the same way in etc di ran setup upgrade...could you please check this : magento.stackexchange.com/questions/122625/…
    – Sushivam
    Jun 24, 2016 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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