7

How to override core class which is already overrided by other custom module

I have module1 and module2 overriding CatalogSearch module.

in Module1

etc/di

<preference for="Magento\CatalogSearch\Controller\Result\Index" type="Vendor\Module1\Controller\Result\Index" />

in Controller class

class Index extends \Magento\CatalogSearch\Controller\Result\Index
{
 public function __construct(
    Context $context,
    Session $catalogSession,
    StoreManagerInterface $storeManager,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    Resolver $layerResolver,
    QueryFactory $queryFactory


) {
     parent::__construct($context,$catalogSession,$storeManager,$resultPageFactory,$queryFactory,$layerResolver);
     $this->resultPageFactory = $resultPageFactory;
     $this->layerResolver = $layerResolver;
     $this->_queryFactory = $queryFactory;
     $this->_storeManager = $storeManager;
}

In Module2

etc/di

<preference for="Vendor\Module1\Controller\Result\Index" type="Vendor\Module2\Controller\Result\Index" />

In Controller class

 class Index extends \Vendor\Module1\Controller\Result\Index
 {
    public function __construct(
    Context $context,
    Session $catalogSession,
    StoreManagerInterface $storeManager,
    Http $request,
    LayoutInterface $layout,
    ScopeConfigInterface $scopeConfig,
     Resolver $layerResolver,     
    \Magento\Catalog\Model\CategoryFactory $catalogCategoryFactory,
    \Magento\Framework\Registry $registry,
    \Vendor\Module1\Model\Client\Connector $tglssearchClientConnector,
    QueryFactory $queryFactory,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
    parent::__construct($context,$catalogSession,$storeManager,$queryFactory,$layerResolver);
        $this->_storeManager = $storeManager;
        $this->catalogSession = $catalogSession;
        $this->storeManager = $storeManager;
        $this->request = $request;
        $this->layout = $layout;
        $this->scopeConfig = $scopeConfig;
        $this->catalogCategoryFactory = $catalogCategoryFactory;
        $this->registry = $registry;
        $this->tglssearchClientConnector = $tglssearchClientConnector;
        $this->layerResolver = $layerResolver;
        $this->_queryFactory = $queryFactory;
        $this->resultPageFactory = $resultPageFactory;

    /* parent::__construct(
        $context
    ); */
}

I get below error,

Argument 4 passed to Vendor\Module1\Controller\Result\Index::__construct() must be an instance of Magento\Framework\View\Result\PageFactory, instance of Magento\Search\Model\QueryFactory given, called in C:\xampp\htdocs\magento2x_5test\app\code\Vendor\Module2\Controller\Result\Index.php on line 36 and defined in C:\xampp\htdocs\magento2x_5test\app\code\Vendor\Module1\Controller\Result\Index.php on line 22

2 Answers 2

4

Module ONE is overriding some core class, if you want to override same core class already used/override module ONE, override Module ONE class by using your module( Module Two )as below example.

Module ONE as Vendor/Module

Vendor/Module is extending Magento\CatalogSearch\Block\Result

In di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\CatalogSearch\Block\Result" type="Vendor\Module\Block\Result"/>
</config>

In Block result.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Module\Block;


/**
 * Product search result block
 */
class Result extends \Magento\CatalogSearch\Block\Result
{
   public function getSearchQueryText()
    {

die('Module ONE');
// Some code
return parent::getSearchQueryText();
    }

}

Module TWO as Vendor/Moduletwo

Vendor/Moduletwo is extending Vendor\Module\Block\Result

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Vendor\Module\Block\Result" type="Vendor\Moduletwo\Block\Result"/>
</config>

In block result.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Moduletwo\Block;


/**
 * Product search result block
 */
class Result extends \Vendor\Module\Block\Result
{
   public function getSearchQueryText()
    {

die('TWO');
// Some code
return parent::getSearchQueryText();
    }

}

In this case both extension functionalities will work.

2
0

To solve this error, correct the sequence of the arguments in the parent::__construct(arg1, arg2, . . .) method. It must be same as it is in the parent's __construct().

so, to remove this error :

change

parent::__construct($context, $catalogSession, $storeManager, $queryFactory, $layerResolver);

to

parent::__construct($context, $catalogSession, $storeManager, $resultPageFactory, $layerResolver, $queryFactory);

this should solve the mentioned error.

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.