1

I wanted to make changes in _prepareLayout() in block. Code is as follow:

Block: Magento\Catalog\Block\Category\View

protected function _prepareLayout()
{
    parent::_prepareLayout();

    $this->getLayout()->createBlock(\Magento\Catalog\Block\Breadcrumbs::class);

    $category = $this->getCurrentCategory();
    if ($category) {
        $title = $category->getMetaTitle();
        if ($title) {
            $this->pageConfig->getTitle()->set($title);
        }
        $description = $category->getMetaDescription();
        if ($description) {
            $this->pageConfig->setDescription($description);
        }
        $keywords = $category->getMetaKeywords();
        if ($keywords) {
            $this->pageConfig->setKeywords($keywords);
        }
        if ($this->_categoryHelper->canUseCanonicalTag()) {
            $this->pageConfig->addRemotePageAsset(
                $category->getUrl(),
                'canonical',
                ['attributes' => ['rel' => 'canonical']]
            );
        }

        $pageMainTitle = $this->getLayout()->getBlock('page.main.title');
        if ($pageMainTitle) {
            $pageMainTitle->setPageTitle($this->getCurrentCategory()->getName());
        }
    }

    return $this;
}

As per above code, I want to make change in below data:

if ($this->_categoryHelper->canUseCanonicalTag()) {
      $this->pageConfig->addRemotePageAsset(
          $category->getUrl(),
          'canonical',
          ['attributes' => ['rel' => 'canonical']]
      );
}

Help me to change the parameter passed to addRemotePageAsset() by overriding block.

0
1

you can not override protected methods in Magento 2, only the public method can be overridden using plugin or preference. there is no way to override protected and private method in Magento 2.

6
  • So how can i add custom code to _prepareLayout() as i have mentioned in my code?
    – Parthavi
    Mar 14 '19 at 5:17
  • using preference we can rewrite the whole class, I don't know about what you've mentioned. Give me any document where it shows it cannot possible to override a protected function Mar 14 '19 at 5:18
  • @Parthavi have a look at it magento.stackexchange.com/questions/126260/… Mar 14 '19 at 5:20
  • 1
    @PrathapGunasekaran u mean that we can use preference for overwriting protected method
    – Parthavi
    Mar 14 '19 at 5:24
  • Yes we can use it. Mar 14 '19 at 5:24
1

In your custom module (lets assume the module is in app/code/Extend/Simple)

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\Category\View" type="Extend\Simple\Block\CategoryView" />
</config>

create class "CategoryView" under Extend\Simple\Block

<?php declare(strict_types=1);

namespace Extend\Simple\Block;

class CategoryView extends \Magento\Catalog\Block\Category\View
{
    protected function _prepareLayout()
    {
       //Your function goes here
    }
}

run setup upgrade

or

cache flush & setup di compile

2
  • It's very important to remove parent::_prepareLayout(); from your _prepareLayout() method.
    – WaPoNe
    May 27 at 14:53
  • @WaPoNe removing means you are throwing off the old functionality, it's not always a good idea to discard if fully, it depends on the business needs. Jun 1 at 10:38

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.