0

I have made a grid in admin that is supposed to return a filtered collection based on a URL parameter lesson_id.

DataProvider:

<?php

namespace VendorName\ModuleName\Ui\DataProvider\Participants;

use Magento\Framework\App\Request\Http;
use VendorName\ModuleName\Model\ResourceModel\Participants\CollectionFactory;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        Http $request,
        CollectionFactory $collectionFactory,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->request = $request;
        $this->collection = $collectionFactory->create();
    }

    public function getData(){
        $lessonId = $this->request->getParam('lesson_id');

        if (!$this->getCollection()->isLoaded()) {
            $this->getCollection()->addFieldToFilter('lesson_id',['eq' => $lessonId]);
            $this->getCollection()->load();
        }

        return $this->getCollection()->toArray();
    }
}

When i var_dump() the Array with lesson_id=7 from the URL it returns:

array(2) { ["totalRecords"]=> int(1) ["items"]=> array(1) { [0]=> array(7) { ["participant_id"]=> string(1) "2" ["lesson_id"]=> string(1) "7" ["customer_id"]=> string(2) "98" ["name"]=> string(8) "test 420" ["completed"]=> string(1) "1" ["created_at"]=> string(19) "2022-10-06 08:41:52" ["updated_at"]=> string(19) "2022-10-07 10:28:35" } } }

However the grid does not display the entry. But now i replace the lesson_id in addFieldToFilter with a normal int or string like so:

$this->getCollection()->addFieldToFilter('lesson_id',7);

The var_dump() shows the exact same array but this time the entry does get displayed in grid without problems. So i have two questions, the first is what causes this and the second is there a workaround?

2 Answers 2

0

This might not be the exact answer you are looking for, but filtering by url params is already supported by the datasource component without creating a custom class.

By adding the filterUrlParams as part of the settings, like this:

<dataSource name="test_data_source" component="Magento_Ui/js/grid/provider">
    <settings>
        <updateUrl path="mui/index/render"/>
        <filterUrlParams>
            <param name="lesson_id">*</param>
        </filterUrlParams>
    </settings>
    ....
</dataSource>

So there is a good chance you can solve it a lot easier.

1
  • Yes i tried that first but that did not filter anything i just got the full database table displayed in grid. Oct 10, 2022 at 7:16
0

for posterity: i still dont know why the URL param gets lost in the data source but i have found a workaround.

<?php
namespace Vendor\Module\Controller\Adminhtml\Participants;

use Magento\Backend\Model\Session;

class Index extends \Magento\Backend\App\Action
{
    public function __construct(
        Session $backendSession
    ) {
        $this->backendSession = $backendSession;
    }

    public function execute()
    {
        $this->backendSession->setTokenId($this->test->getUrlParam('lesson_id'));

    }
}

here we get the URL param in the grid controller and store it in the back end session.

<?php

namespace VendorName\ModuleName\Ui\DataProvider\Participants;

use VendorName\ModuleName\Model\ResourceModel\Participants\CollectionFactory;
use Magento\Backend\Model\Session;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        Session $backendSession,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->collection = $collectionFactory->create();
        $this->backendSession = $backendSession;
    }

    public function getData(){
        if (!$this->getCollection()->isLoaded()) {
            $this->getCollection()->addFieldToFilter('lesson_id',['eq' => $this->backendSession->getTokenId()]);
            $this->getCollection()->load();
        }

        return $this->getCollection()->toArray();
    }
}

and now we retrieve the url param from the session in the data-provider and now the filter is applied properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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