1

I have a custom overriden class say Result.php overridden from CatalogSearch module.

Now if i have to use this Result class in 2 different modules(Module1 and Module2), do i have to override the class in both the modules (in the 'type' attribute of di)??

etc/di of Module1:

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

etc/di of Module2:

<preference for="Magento\CatalogSearch\Block\Result" type="Vendor/Module2/Block/Result">

In the above case, if i want to track as to which Result from my custom module is getting called?

PROBLEM:

I tried echo in Module1's Result class somefun(), i get it, but i comment out the same and try in Module2's Result class somefun(), i dont get

Why?

Can i use something like this in Module2 di.

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

I gave module1 in type

2
  • So @Krishna, as per your explanation should my module2's di be like this? <preference for="Vendor/Module2/Block/Result" type="Vendor/Module1/Block/Result">
    – Sushivam
    Commented Aug 16, 2016 at 8:16
  • But all my functions in both the classes are same
    – Sushivam
    Commented Aug 16, 2016 at 8:16

1 Answer 1

0

You can try below code

UPDATED

<preference for="Vendor\Module2\​Block\Result" type="Vendor\Module1‌\Block\Result">

Do not use all functions unless you need to modify, if function is not found it will look into parent class.

I have added a small example to have the basic fallback.

  1. First module (Vendor\Module)

\app\code\Vendor\Module\etc\frontend\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>

Create Block

\app\code\Vendor\Module\Block\Result.php

<?php
namespace Vendor\Module\Block;
class Result extends \Magento\CatalogSearch\Block\Result
{
        protected function _prepareLayout()
    {
        echo "Vendor\Module";
        exit;

        //return parent::_prepareLayout();

    }
}
  1. Second module (Vendor\Moduletwo)

\app\code\Vendor\Moduletwo\etc\frontend\di.xml

<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>

Create Block

\app\code\Vendor\Moduletwo\Block\Result.php

<?php

namespace Vendor\Moduletwo\Block;
class Result extends \Vendor\Module\Block\Result
{
    protected function _prepareLayout()
    {
        echo "Vendor\Moduletwo";
        exit;

        //return parent::_prepareLayout();
    }
}

Run setup-upgrade, remove cache and search some string in header mini search.

Please consider this is an example.

Hope this helps.

4
  • Does Magento\CatalogSearch\Block\Result comes under preference or type?
    – Sushivam
    Commented Aug 16, 2016 at 9:37
  • in line1 while overriding 1st module, preference for should be Magento\CatalogSearch\Block\Result rite? @Krishna
    – Sushivam
    Commented Aug 16, 2016 at 10:44
  • i use the above @Krishna, still my 1st module class is getting called!
    – Sushivam
    Commented Aug 16, 2016 at 11:17
  • Ill try and update @Krishna
    – Sushivam
    Commented Aug 17, 2016 at 8:01

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.