I am adding pagination block to my custom shop by brand module, here is Block file
<?php
namespace PinkBlue\ShopByBrand\Block;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
class View extends \Magento\Framework\View\Element\Template
{
protected $_defaultToolbarBlock = 'Magento\Catalog\Block\Product\ProductList\Toolbar';
/**
* @var \Magento\Framework\App\Http\Context
*/
protected $httpContext;
/**
* Catalog product visibility
*
* @var \Magento\Catalog\Model\Product\Visibility
*/
protected $_catalogProductVisibility;
/**
* Product collection factory
*
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $_productCollectionFactory;
protected $_productCollection;
/**
* Image helper
*
* @var Magento\Catalog\Helper\Image
*/
protected $_imageHelper;
/**
* @var \Magento\Checkout\Helper\Cart
*/
protected $_cartHelper;
protected $_brandFactory;
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Magento\Framework\App\Http\Context $httpContext,
\PinkBlue\ShopByBrand\Model\BrandFactory $brandFactory,
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory;
$this->_catalogProductVisibility = $catalogProductVisibility;
$this->httpContext = $httpContext;
$this->_imageHelper = $context->getImageHelper();
$this->_brandFactory = $brandFactory;
$this->_cartHelper = $context->getCartHelper();
parent::__construct(
$context,
$data
);
$this->setCollection($this->getProductCollection());
}
public function getAddToCartUrl($product, $additional = [])
{
return $this->_cartHelper->getAddUrl($product, $additional);
}
public function _prepareLayout()
{
parent::_prepareLayout();
$brand = $this->getBrand();
$brand_url = ($brand->getUrlKey())?
$this->getUrl('brand').$brand->getUrlKey():
$this->getUrl('brand/view/',array('id'=>$brand->getId()));
$this->pageConfig->addRemotePageAsset(
$brand_url,
'canonical',
['attributes' => ['rel' => 'canonical']]
);
//sup code for pagination
if ($this->getCollection()) {
$pager = $this->getLayout()->createBlock(
'Magento\Theme\Block\Html\Pager',
'reward.history.pager');
$pager->setLimit(36)
->setShowAmounts(false)
->setCollection($this->getCollection());
$this->setChild('pager', $pager);
$this->getCollection()->load();
}
//Sup code ends
return $this;
}
/**
* @return string
*/
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
public function getBrand(){
$id = $this->getRequest()->getParam('id');
if ($id) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->create('PinkBlue\ShopByBrand\Model\Items');
$model->load($id);
return $model;
}
return false;
}
public function getProductCollection()
{
if($this->_productCollection===NULL) {
$brand = $this->getBrand();
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addAttributeToSelect('name');
$collection->addStoreFilter()->addAttributeToFilter('manufacturer', $brand->getAttributeId());
$collection->addAttributeToFilter('status', Status::STATUS_ENABLED);
$collection->addAttributeToFilter('visibility', array('neq' => \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE));
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
public function imageHelperObj(){
return $this->_imageHelper;
}
public function getProductPricetoHtml(
\Magento\Catalog\Model\Product $product,
$priceType = null
) {
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
$price = '';
if ($priceRender) {
$price = $priceRender->render(
\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
$product
);
}
return $price;
}
/**
* Retrieve list toolbar HTML
*
* @return string
*/
public function getToolbarHtml()
{
return $this->getChildHtml('toolbar');
}
/**
* Retrieve additional blocks html
*
* @return string
*/
public function getAdditionalHtml()
{
return $this->getChildHtml('additional');
}
/**
* Retrieve Toolbar block
*
* @return \Magento\Catalog\Block\Product\ProductList\Toolbar
*/
public function getToolbarBlock()
{
$blockName = $this->getToolbarBlockName();
if ($blockName) {
$block = $this->getLayout()->getBlock($blockName);
if ($block) {
return $block;
}
}
$block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
return $block;
}
/**
* Retrieve current view mode
*
* @return string
*/
public function getMode()
{
return $this->getChildBlock('toolbar')->getCurrentMode();
}
/**
* Need use as _prepareLayout - but problem in declaring collection from
* another block (was problem with search result)
* @return $this
*/
protected function _beforeToHtml()
{
$toolbar = $this->getToolbarBlock();
// called prepare sortable parameters
$collection = $this->getProductCollection();
// use sortable parameters
$orders = $this->getAvailableOrders();
if ($orders) {
$toolbar->setAvailableOrders($orders);
}
$sort = $this->getSortBy();
if ($sort) {
$toolbar->setDefaultOrder($sort);
}
$dir = $this->getDefaultDirection();
if ($dir) {
$toolbar->setDefaultDirection($dir);
}
$modes = $this->getModes();
if ($modes) {
$toolbar->setModes($modes);
}
// set collection to toolbar and apply sort
$toolbar->setCollection($collection);
$this->setChild('toolbar', $toolbar);
$this->getProductCollection()->load();
return parent::_beforeToHtml();
}
}
I want to make the limit dynamic but here i have set it as
$pager->setLimit(36)
How Can I achieve this?