These are my block methods:
public function getProductsViewHistory() {
$page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
$pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 1;
$fromDate = date('Y-m-d H:i:s', strtotime("-6 month"));
$product = $this->customerProductViewHistory->create();
$product->addFieldToFilter('subject_id',array('eq'=> $this->getLoggedInUserId() ))
->addFieldToFilter('logged_at',array('from'=> $fromDate ))
->addFieldToSelect('object_id')
->getSelect()->group('object_id');
$product->setPageSize( self::PRODUCTS_PER_PAGE );
$product->setCurPage($page);
return $product;
}
protected function _prepareLayout()
{
parent::_prepareLayout();
// $this->pageConfig->getTitle()->set(__('News'));
if ($this->getProductsViewHistory()) {
$pager = $this->getLayout()->createBlock(
'Magento\Theme\Block\Html\Pager',
'history.pager'
)->setAvailableLimit(array( self::PRODUCTS_PER_PAGE=>self::PRODUCTS_PER_PAGE))
->setShowPerPage(true)->setCollection( $this->getProductsViewHistory() );
$this->setChild('pager', $pager);
$this->getProductsViewHistory()->load();
}
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
And this is my template file.
<?php if ($block->getPagerHtml()): ?>
<div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
<?php endif ?>
<?php
$collection = $this->getCollection();
foreach ($collection as $key => $value) {
var_dump($value->getData());
}
?>
I get this error:
Warning: Invalid argument supplied for foreach()
If I comment out the part to show collection, I'm successfully getting pagination as intended.
What am I missing?