0

I use backend option to set an url for my page, now i want to add pager to that page:

$pager = $this->getLayout()
    ->createBlock(
        'Magento\Theme\Block\Html\Pager',
        'module.custom.pager'
    )->setAvailableLimit(array(2=>2))
    ->setShowPerPage(true)
    ->setCollection(
        $this->getCustomCollection()
);
$this->setChild('pager', $pager);

this code adds pager but it shows module URL(specified in route.xml) URL if we use pager and not the specified one.

public function match(\Magento\Framework\App\RequestInterface $request)
{
    $path = trim($request->getPathInfo(), '/');
    $array = explode('/', $path);
    if(strpos($path, $this->getFrontendUrl()) !== false) {
        $request->setModuleName('modulename')
            ->setControllerName('index')
            ->setActionName('index');
        if(isset($array[1])){
            $request->setParam('param', $array[1]);
        }
   } else {
       return false;
   }
   return $this->actionFactory->create(
       'Magento\Framework\App\Action\Forward',
       ['request' => $request]
   );
}
/**
 * @return string
 */
public function getFrontendUrl(){
    return $this->_scopeConfig->getValue('section/group/customurl', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
}

one option which I'm aware of is to use URL rewrites, is there any other way?

  • 1
    please explain briefly – Rk Rathod Jun 19 at 12:52
  • I have url which we set in backend for module, pager uses url specified in routes.xml. need that pager would use url from backend and not from routes.xml – Naberd Zendi Jun 19 at 13:16
  • added my code to repository: github.com/Naberdz/producttags you can see the problem when you visit tag page and try to use pager. – Naberd Zendi Jun 19 at 14:58
0

Fixed my problem by creating custom pager and adjusted getPagerUrl function to match needed result

controller _prepareLayout function to initialize pager:

$pager = $this->getLayout()
    ->createBlock(
       'Wemessage\ProductTags\Block\CustomPager',
       'wemessage.tags.pager'
    )->setAvailableLimit(array(2=>2))
    ->setShowPerPage(true)
    ->setCollection(
        $this->getTaggedCollection()
    );
$this->setChild('pager', $pager);
$this->getTaggedCollection()->load();

custom pager :

class CustomPager extends \Magento\Theme\Block\Html\Pager
{

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param \Magento\Framework\Registry $registry
     * @param \Wemessage\ProductTags\Model\TagsFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_scopeConfig = $scopeConfig;
        $this->_storeManager = $storeManager;
    }

    /**
     * @return string
     */
    public function getFrontendSlug(){
        return $this->_scopeConfig->getValue('producttags/options/tagslug', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }

    /**
     * Retrieve page URL by defined parameters
     *
     * @param array $params
     * @return string
     */
    public function getPagerUrl($params = [])
    {
        $urlParams = [];
        $urlParams['_current'] = true;
        $urlParams['_escape'] = true;
        $urlParams['_use_rewrite'] = true;
        $urlParams['_fragment'] = $this->getFragment();
        $urlParams['_query'] = $params;
        return str_replace('producttag/index/index', $this->getFrontendSlug(), $this->getUrl($this->getPath(), $urlParams)); //producttag defined in routes.xml
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.