0

As per my requirement, I need to add a custom grid into a custom tab in the product edit page.

for that, I got one of the good tutorials, but in that, I need to filter the collection by the current product id.

http://www.codextblog.com/magento-2/how-to-add-custom-grid-into-a-custom-tab-in-product-edit-page-in-magento-2-admin/

But right now the grid gets the full collection of the custom model.

In that how can I achieve the filter using the current product id?

Updated code in custom_grid_listing.xml

<dataSource name="questionanswer_grid_listing_data_source" component="Magento_Ui/js/grid/provider">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
            <item name="filter_url_params" xsi:type="array">
                <item name="product_id" xsi:type="string">*</item>
            </item>
            <item name="update_url" xsi:type="url" path="mui/index/render"/>

            <item name="storageConfig" xsi:type="array">
                <item name="cacheRequests" xsi:type="boolean">false</item>
            </item>
        </item>
    </argument>
</dataSource>   

CustomDataProvider.php

Then i am using below code go get the product_id from the URL, but in URL dont have any product realated infromation

$params =  $this->_request->getParams();
$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
echo $urlInterface->getCurrentUrl();
echo "<pre>"; print_r($params); 

enter image description here

Kindly check the details and share your knowledge.

Thanks in advance for your help and support.

2 Answers 2

0

Add following code snippet inside custom_grid_listing_data_source tag under settings:

app/code/Codextblog/Customgridtab/view/adminhtml/ui_component/custom_grid_listing.xml

<filterUrlParams>
    <param name="product_id">*</param>
</filterUrlParams>

Looks like:

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

In that case, your new table should product_id column exists, otherwise will throw an exception.

6
  • thanks for your comments, i followed as per your way, but still i didn't get the product_id from the URL parameter, i updated my question with the code and response screen. Kindly check and share your thoughts
    – senthil
    Commented Apr 9, 2019 at 5:13
  • Can you add your module somewhere with db table will easy fix.
    – Sohel Rana
    Commented Apr 9, 2019 at 5:22
  • i can not able to share the module to any where, apart from that do you need any other details i can
    – senthil
    Commented Apr 9, 2019 at 5:42
  • Hi @senthil, were you able to get past this issue? I'm currently experiencing this exact issue.. Commented May 20, 2019 at 18:32
  • @senthil Did you solve this issue? Commented Nov 8, 2022 at 2:21
0

I fixed this by changing the below in Ui/DataProvider/Product/CustomDataProvider.php

$this->getCollection()->addEntityFilter($this->request->getParam('current_product_id', 0));

and in Ui/DataProvider/Product/Form/Modifier/CustomTab.php

            'imports' => [
                                'productId' => '${ $.provider }:data.product.current_product_id',
                                '__disableTmpl' => ['productId' => false],
                            ],
                            'exports' => [
                                'productId' => '${ $.externalProvider }:params.current_product_id',
                                '__disableTmpl' => ['productId' => false],
                            ],

You can compare the module with vendor/magento/module-review module. It has similar functionality

Your Answer

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

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