0

I want to override the protected function _prepareLayout()

So i have used preference

<preference for="Magento\Catalog\Block\Category\View" type="Module\Name\Block\Category\View"/>
class View extends \Magento\Catalog\Block\Product\View
{
    protected $_urlInterface;


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Framework\UrlInterface $urlInterface,
        array $data = []
    ) {
        $this->_urlInterface = $urlInterface;
        parent::__construct($context, $layerResolver, $registry, $categoryHelper, $data);
    }

    /**
     * @return $this
     */
    protected function _prepareLayout()
    {
        die(var_dump($this));
        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(),
                    $this->_urlInterface->getCurrentUrl(),
                    'canonical',
                    ['attributes' => ['rel' => 'canonical']]
                );
            }

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

        return $this;
    }

but i get error 500

PHP Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Catalog\Block\Product\View::__construct() must be an instance of Magento\Catalog\Block\Product\Context, instance of Magento\Framework\View\Element\Template\Context

4
  • What you want to achieve by override that file? Nov 21, 2019 at 12:53
  • insted of calling this => $category->getUrl(), I want it to call ->$this->_urlInterface->getCurrentUrl(), that's all Adding new DI to consturct and call $this->_urlInterface->getCurrentUrl(), Nov 21, 2019 at 12:55
  • which magetn version, you are using?
    – Amit Bera
    Nov 21, 2019 at 13:00
  • magento v:2.2.10 Nov 21, 2019 at 13:01

1 Answer 1

1

You just need to replace your construct function with this

<?php

namespace Vendor\Module\Block\Category;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Category;

class View extends \Magento\Catalog\Block\Category\View
{
    protected $_urlInterface;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Framework\UrlInterface $urlInterface,
        array $data = []
    ) {
        $this->_urlInterface = $urlInterface;
        $this->_categoryHelper = $categoryHelper;
        $this->_catalogLayer = $layerResolver->get();
        $this->_coreRegistry = $registry;
        parent::__construct($context, $layerResolver, $registry, $categoryHelper, $data);
    }

    protected function _prepareLayout()
    {
        echo "123";die;
        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;
    }
}

Then run below commands once

rm -rf generated/* var/view_preprocessed/*
php bin/magento setup:upgrade
php bin/magento cache:clean
php bin/magento cache:flush

Hope this will help you!

14
  • Still getting the same error PHP message: PHP Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Catalog\Block\Product\View::__construct() must be an instance of Magento\Catalog\Block\Product\Context, instance of Magento\Framework\View\Element\Template\Context Nov 21, 2019 at 12:59
  • Did you run all commands ? after changing construct ? Nov 21, 2019 at 13:00
  • Yes i did php bin/magento setup:upgrade php bin/magento cache:clean php bin/magento cache:flush and i did rm -rf var/di/* var/generation/* var/cache/* var/log/* var/page_cache/* var/session/* var/view_preprocessed/* pub/static/* Nov 21, 2019 at 13:01
  • 1
    this echo worked :) what was the error? Nov 21, 2019 at 13:13
  • 1
    If you'll check parent file's construct function then they are passing 5 arguments in params and you just passed $contect and $data. So if you'll pass all other then it will work. And whenever you will add anything in construct at that time you need to run setup upgrade or di compile once. :) Nov 21, 2019 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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