I want to override a _prepareLayout function of the block module-catalog-search/Block/Result.php .

 protected function _prepareLayout()
        $title = $this->getSearchQueryText();
        // add Home breadcrumb
        $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
        if ($breadcrumbs) {
                    'label' => __('Home'),
                    'title' => __('Go to Home Page'),
                    'link' => $this->_storeManager->getStore()->getBaseUrl()
                ['label' => $title, 'title' => $title]

        return parent::_prepareLayout();

In above function, if I want to change the breadcrumbs name from Home > Search Result to something else. How can I do it with the help of plugin ?

  • Dinesh Yadav, in default m2...... "Search results" coming . whereSearch comming?
    – Amit Bera
    Mar 14, 2017 at 5:32
  • I was just giving the example. If it's already "Search results", Can you please tell me how to change that text "search results" ? . I just want to change it with the help of plugin. Mar 14, 2017 at 5:36

4 Answers 4


Easiest solution is that use translations

Translation is simple way with doing less code:

Create a translation file at theme translation folder


and write code

"Search results for: '%1'", "My results for: '%1'"

See how use http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/translations/translate_practice.html


Earlier I didn't know that Plugin cannot override protected functions. A plugin can override only public functions. So it's not possible to override given method using a plugin.


This can be done by two ways:

1) using Preference

2) using Plugin

Preference is similar to class rewrite in Magento 1. There is always possibility of conflict when two or more custom modules try to rewrite/override same core class.

There is Plugin to address the limitation of Preference. With Plugin, we can execute our code before, after and around the core/target class’s function. We don’t replace the core/target class’s code/function. We just add some code before/after the core code. It’s somehow similar to event observer. We just observe the core/target class’s function and execute our code in-between the core/target class’s function. Multiple modules can use their own plugins to add their own code before/after/around the same core/target class’s function.

Using Preference

Here are the examples of rewriting Block, Model, Controller, and Helper class using Preference. I suppose the module name as YourCompany_YourModule.

We have to define preference in in app/code/YourCompany/YourModule/etc/di.xml. There we define the core/target class which we want to rewrite. We also define our module’s class that is going to rewrite the core/target class.



<?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\View" type="YourCompany\YourModule\Block\Catalog\Product\View" />

Let’s rewrite getProduct() function of class Magento\Catalog\Block\Product\View. We will just log some message on var/log/debug.log for this test.



namespace YourCompany\YourModule\Block\Catalog\Product;

class View extends \Magento\Catalog\Block\Product\View
     * Retrieve current product model
     * @return \Magento\Catalog\Model\Product
    public function getProduct()
        // logging to test override    
        $logger = \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface');
        $logger->debug('Block Override Test');

        if (!$this->_coreRegistry->registry('product') && $this->getProductId()) {
            $product = $this->productRepository->getById($this->getProductId());
            $this->_coreRegistry->register('product', $product);
        return $this->_coreRegistry->registry('product');

If you want to override block with Plugin, make sure the methods you want to use Plugin are not:

  • Final methods
  • Final classes
  • Non-public methods
  • Class methods (such as static methods)
  • __construct and __destruct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped
  • Objects that implement Magento\Framework\ObjectManager\NoninterceptableInterface

Reference: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html

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.