Magento – Catalog Category not loading with pager

magento2

Whats wrong in my overriden Catalog module.

Catalog is not rendering in frontend

etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Controller\Category\View" type="Vendor\Module\Controller\Category\View" />
<preference for="Magento\Catalog\Block\Product\ListProduct" type="Vendor\Module\Block\Product\ListProduct" />
<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Vendor\Module\Block\Product\ProductList\Toolbar" />
<preference for="Magento\Catalog\Model\ResourceModel\Product\Collection" type="Vendor\Module\Model\ResourceModel\Product\Collection" />

\Vendor\Module\Block\Product\ProductList\Toolbar.php

namespace Vendor\Module\Block\Product\ProductList;
use Magento\Catalog\Helper\Product\ProductList;
use Magento\Catalog\Model\Product\ProductList\Toolbar as ToolbarModel;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;



class Toolbar extends \Magento\Framework\View\Element\Template
{
     protected $tglssearchHelper;
    protected $scopeConfig;
    protected $_availableMode = [];
    protected $_direction = ProductList::DEFAULT_SORT_DIRECTION;
    protected $_isExpanded = true;
    protected $_enableViewSwitcher = true;
    protected $_orderField = null;
    protected $_collection = null;

protected function _prepareLayout()
{
    parent::_prepareLayout();
    if ($this->getCollection()) {
        // create pager block for collection 
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'tgl.pager'
        )->setCollection(
            $this->getCollection() // assign collection to pager
        );
        $this->setChild('product_list_toolbar_pager', $pager);// set pager block in layout
    }
    return $this;
}


public function getCollection()
{
    return $this->_collection;
}

public function getPagerHtml()
{
    $pagerBlock = $this->getChildBlock('product_list_toolbar_pager'); 
    if ($pagerBlock instanceof \Magento\Framework\DataObject) {
        /* @var $pagerBlock \Magento\Theme\Block\Html\Pager */
        $pagerBlock->setAvailableLimit($this->getAvailableLimit());

        $pagerBlock->setUseContainer(
            false
        )->setShowPerPage(
            false
        )->setShowAmounts(
            false
        )->setFrameLength(
            $this->_scopeConfig->getValue(
                'design/pagination/pagination_frame',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            )
        )->setJump(
            $this->_scopeConfig->getValue(
                'design/pagination/pagination_frame_skip',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            )
        )->setLimit(
            $this->getLimit()
        )->setCollection(
            $this->getCollection()
        );

        return $pagerBlock->toHtml();
    }

    return '';
}

\Vendor\Module\view\frontend\layout\catalog_category_view.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock class="Vendor\Module\Block\Product\ProductList\Toolbar" before="-" name="product_list_toolbar">
      <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendor_Module::product/list/toolbar.phtml</argument>
      </action>
    </referenceBlock>
</body>

in my view\templates folder: PROBLEM HERE

If i copy core toolbar.phtml in the view\frontend\templates\product\list\toolbar.phtml then i get as below:

enter image description here

If i remove the above phtml then i get the products but without pagination as below: (with error : Invalid template file: 'Vendor_Module::product/list/toolbar.phtml' in module: 'Vendor_Module' block's name: 'product_list_toolbar' [] [])

— No Pagination here

enter image description here

Best Answer

Finally i got a solution for the same.

Added the below function in the Toolbar.php

protected function _prepareLayout()
{
    //echo 'prepare Layout';die;
     parent::_prepareLayout();
    if ($this->getCollection()) {
        // create pager block for collection 
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'tgl.pager'
        )->setCollection(
            $this->getCollection() // assign collection to pager
        );
        $this->setChild('pager', $pager);// set pager block in layout
    } 

    return $this;
}

 public function getPagerHtml()
{
    //echo 'getpagerhtml';die;
     $pagerBlock = $this->getChildBlock('product_list_toolbar_pager'); //print_r($pagerBlock);die;

    if ($pagerBlock instanceof \Magento\Framework\DataObject) {
        // @var $pagerBlock \Magento\Theme\Block\Html\Pager 
        $pagerBlock->setAvailableLimit($this->getAvailableLimit());

        $pagerBlock->setUseContainer(
            false
        )->setShowPerPage(
            false
        )->setShowAmounts(
            false
        )->setFrameLength(
            $this->_scopeConfig->getValue(
                'design/pagination/pagination_frame',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            )
        )->setJump(
            $this->_scopeConfig->getValue(
                'design/pagination/pagination_frame_skip',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            )
        )->setLimit(
            $this->getLimit()
        )->setCollection(
            $this->getCollection()
        );

        return $pagerBlock->toHtml();
    } 

    return '';
    //return $this->getChildHtml('pager');
}

Added toolbar.phtml in view/frontend/templates/product/list/

Related Topic