I created an admin grid using virtual type in di.xml like this:

<virtualType name="Namespace\Module\Model\ResourceModel\Test\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
      <argument name="mainTable" xsi:type="string">module_test</argument>
      <argument name="resourceModel" xsi:type="string">Namespace\Module\Model\ResourceModel\Test\Collection</argument>

i want to filter the data by the id that passed via an url like this:


I want my grid to load only data that contain only test_id which sent via url

<column name="test_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" translate="true" xsi:type="string">test_id</item>

2 Answers 2


if you have this in your ui component :

<item name="update_url" path="mui/index/render" xsi:type="url"/>

then add this code below above code:

<item name="filter_url_params" xsi:type="array"> 
     <item name="test_id" xsi:type="boolean">1</item>

Instead of using a virtual type, you need to create a class which extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult and has a _beforeLoad method where the test_id filter is applied. Example:

use Magento\Framework\App\RequestInterface;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Psr\Log\LoggerInterface as Logger;

class Collection extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
    protected $request;

    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'module_test',
        $resourceModel = 'Namespace\Module\Model\ResourceModel\Test\Collection',
        RequestInterface $request
    ) {
        $this->request = $request;

        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);

    public function _beforeLoad()
        // show only items with passed in parameter
        if ($testId = $this->request->getParam('test_id')) {
            $this->addFieldToFilter('test_id', $testId);

        return parent::_beforeLoad();
  • Nice answer. However I have to mention that dynamic variable from request doesn't work in dataprovider (tested in magento 2.3). I have to get value from request in controller and store into session. Example answer here magento.stackexchange.com/questions/259044/…
    – electroid
    Jun 25, 2020 at 20:37

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.