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