Override Constructor of Vendor/Magento/Module_Catalog/Block in Magento 2

blocksmagento2

I am trying to get product list from specific category id.
i am using magento 2.2.1 version.

below are my code of
Block File : view.php
i want to override this file from vender.

Here is my custom module :

app\code\Narola\Magentocatalog\Block\Category\View.php

namespace Narola\Magentocatalog\Block\Category;
class View extends \Magento\Catalog\Block\Category\View
{
  protected $_productCollectionFactory;
  protected $_categoryFactory;

  public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    array $data = []
  ) {
    $this->_categoryFactory = $categoryFactory;
    $this->_productCollectionFactory = $productCollectionFactory;
    parent::__construct($context, $data);
}

public function getSubcategory($catId)  
{
    $objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
    $categoryFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
    $collection = $categoryFactory->create()
    ->addFieldToFilter('parent_id', $catId) 
    ->addAttributeToSelect('*');
    return $collection;
}

public function  getSubcategory_product($sub_catid){
    $category = $this->_categoryFactory->create()->load($categoryId);
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addCategoryFilter($category);
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    return $collection;
} 
}

==============================

Here when i add constructor i am getting below error.


Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Catalog\Block\Category\View::__construct() must be an instance of Magento\Catalog\Model\Layer\Resolver, array given, called in C:\wamp64\www\magento_221\app\code\Narola\Magentocatalog\Block\Category\View.php on line 18 and defined in C:\wamp64\www\magento_221\vendor\magento\module-catalog\Block\Category\View.php on line 45

If any one has solution regarding this then please provide me.

Best Answer

Your constructor has to be like

\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Helper\Category $categoryHelper,
array $data = []

and call the parent function like

parent::__construct($context, $layerResolver, $layerResolver, $categoryHelper, $data);