2

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

 protected function _prepareLayout()
    {
        $title = $this->getSearchQueryText();
        $this->pageConfig->getTitle()->set($title);
        // add Home breadcrumb
        $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
        if ($breadcrumbs) {
            $breadcrumbs->addCrumb(
                'home',
                [
                    'label' => __('Home'),
                    'title' => __('Go to Home Page'),
                    'link' => $this->_storeManager->getStore()->getBaseUrl()
                ]
            )->addCrumb(
                'search',
                ['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 ?

2
  • Dinesh Yadav, in default m2...... "Search results" coming . whereSearch comming? – Amit Bera Mar 14 '17 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. – Dinesh Yadav Mar 14 '17 at 5:36
3

Easiest solution is that use translations

Translation is simple way with doing less code:

Create a translation file at theme translation folder

app/design/frontend/[VendorName]/[Theme]/i18n/[Your_Lanague_Code].csv

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

0
3

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.

0

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.

BLOCK OVERRIDE

app/code/YourCompany/YourModule/etc/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\View" type="YourCompany\YourModule\Block\Catalog\Product\View" />
</config>

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.

app/code/YourCompany/YourModule/Block/Catalog/Product/View.php

<?php

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');
    }
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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