2

This is Tab "Staff Manager" have display but can't receive Product Id enter image description here view/adminhtml/ui_component /staff_listing.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
    <item name="js_config" xsi:type="array">
        <item name="provider" xsi:type="string">staff_listing.staff_listing_data_source</item>
        <item name="deps" xsi:type="string">staff_listing.staff_listing_data_source</item>
    </item>
    <item name="spinner" xsi:type="string">staff_columns</item>
</argument>
<dataSource name="staff_listing_data_source" component="Magento_Ui/js/grid/provider">
    <settings>
        <storageConfig>
            <param name="cacheRequests" xsi:type="boolean">false</param>
        </storageConfig>
        <updateUrl path="test"/>
    </settings>
    <dataProvider class="Custom\Staff\Ui\DataProvider\StaffDataProvider" name="staff_listing_data_source">
        <settings>
            <requestFieldName>id</requestFieldName>
            <primaryFieldName>id</primaryFieldName>
        </settings>
    </dataProvider>
</dataSource>
<container name="listing_top">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="template" xsi:type="string">ui/grid/toolbar</item>
            <item name="stickyTmpl" xsi:type="string">ui/grid/sticky/toolbar</item>
        </item>
    </argument>
    <filters name="listing_filters">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="params" xsi:type="array">
                    <item name="filters_modifier" xsi:type="array"/>
                </item>
            </item>
        </argument>
        <settings>
            <statefull>
                <property name="applied" xsi:type="boolean">false</property>
            </statefull>
        </settings>
    </filters>
    <bookmark name="bookmarks">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="namespace" xsi:type="string">staff_listing</item>
                </item>
            </item>
        </argument>
    </bookmark>
    <paging name="listing_paging">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">staff_listing.staff_listing.listing_top.bookmarks</item>
                    <item name="namespace" xsi:type="string">current.paging</item>
                </item>
                <item name="selectProvider" xsi:type="string">staff_listing.staff_listing.amasty_customform_answer_columns.ids</item>
            </item>
        </argument>
    </paging>
</container>
<columns name="staff_columns" class="Magento\Ui\Component\Listing\Columns">
    <settings>
        <childDefaults>
            <param name="fieldAction" xsi:type="array">
                <item name="params" xsi:type="array">
                    <item name="0" xsi:type="string">${ $.$data.rowIndex }</item>
                </item>
            </param>
        </childDefaults>
    </settings>
    <column name="id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="editor" xsi:type="array">
                    <item name="editorType" xsi:type="string">text</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
                <item name="label" xsi:type="string" translate="true">ID</item>
            </item>
        </argument>
    </column>
    <column name="name">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="editor" xsi:type="array">
                    <item name="editorType" xsi:type="string">text</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
                <item name="label" xsi:type="string" translate="true">Name</item>
            </item>
        </argument>
    </column>
</columns>

Ui/DataProvider/StaffDataProvider

    <?php
    namespace Custom\Staff\Ui\DataProvider;
    use Magento\Framework\App\RequestInterface;
    use Magento\Ui\DataProvider\AbstractDataProvider;
    use Custom\Staff\Model\ResourceModel\Staff\CollectionFactory;
    use Magento\Catalog\Controller\Adminhtml\Product\Builder as ProductBuilder;
    use Magento\Ui\DataProvider\Modifier\ModifierInterface;
    use Magento\Ui\DataProvider\Modifier\PoolInterface;
    /**
     * Class StaffDataProvider
     */
    class StaffDataProvider extends AbstractDataProvider
    {
        protected $collection;
        protected $addFieldStrategies;
        protected $addFilterStrategies;
        protected $request;
        protected $registry;
        protected $productBuilder;
        protected $pool;
        public function __construct(
            $name,
            $primaryFieldName,
            $requestFieldName,
            CollectionFactory $collectionFactory,
            RequestInterface $request,
            \Magento\Framework\Registry $registry,
            ProductBuilder $productBuilder,
            PoolInterface $pool,
            array $meta = [],
            array $data = []
        ) {
            parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
            $this->collection = $collectionFactory->create();
            $this->request = $request;
            $this->registry = $registry;
            $this->pool = $pool;
            $this->productBuilder = $productBuilder;
            $this->data = $data;
        }
        public function getCollection(){  
            return $this->collection;
        }
        public function getData(){     
            foreach ($this->pool->getModifiersInstances() as $modifier) {
                $this->data = $modifier->modifyData($this->data);
            }
            return $this->data;
        }
        public function addFilter(\Magento\Framework\Api\Filter $filter)
        {
            $field = $filter->getField();
            parent::addFilter($filter);
        }
        public function getMeta()
        {
            $meta = parent::getMeta();

            foreach ($this->pool->getModifiersInstances() as $modifier) {
                $meta = $modifier->modifyMeta($meta);
            }

            return $meta;
        }
    }

Ui\DataProvider\Product\Form\Modifier

<?php 
namespace Custom\Staff\Ui\DataProvider\Product\Form\Modifier; 
use Magento\Catalog\Model\Locator\LocatorInterface; 
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier; 
use Magento\Framework\Stdlib\ArrayManager; 
use Magento\Framework\UrlInterface; 
use Magento\Ui\Component\Container; 
use Magento\Ui\Component\Form\Fieldset;
use Magento\Ui\DataProvider\Modifier\PoolInterface; 
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Module\Manager as ModuleManager;
class Staff extends AbstractModifier { 
    const SAMPLE_FIELDSET_NAME = 'staff_listing_fieldset'; 
    const SAMPLE_FIELD_NAME = 'staff_listing'; 
    const GROUP_REVIEW = 'staff';
    const GROUP_CONTENT = 'content';
    const SORT_ORDER = 20;
    const LINK_TYPE = 'associated';
    protected $_backendUrl; 
    protected $_productloader; 
    protected $_modelCustomgridFactory; 
    private $moduleManager;
    protected $locator; 
    protected $arrayManager; 
    protected $urlBuilder; 
    protected $meta = [];
    public function __construct( LocatorInterface $locator, ArrayManager $arrayManager, UrlInterface $urlBuilder, \Magento\Catalog\Model\ProductFactory $_productloader, \Magento\Backend\Model\UrlInterface $backendUrl ) { $this->locator = $locator;
        $this->arrayManager = $arrayManager;
        $this->urlBuilder = $urlBuilder;
        $this->_productloader = $_productloader;
        $this->_backendUrl = $backendUrl;
        $this->locator = $locator;
    }
    public function modifyMeta(array $meta){
        if (!$this->locator->getProduct()->getId() || !$this->getModuleManager()->isOutputEnabled('Custom_Staff')) {
            return $meta;
        }
        $meta[static::GROUP_REVIEW] = [
            'children' => [
                'staff_listing' => [
                    'arguments' => [
                        'data' => [
                            'config' => [
                                'autoRender' => true,
                                'componentType' => 'insertListing',
                                'dataScope' => 'staff_listing',
                                'externalProvider' => 'staff_listing.staff_listing_data_source',
                                'selectionsProvider' => 'staff_listing.staff_listing.product_columns.ids',
                                'ns' => 'staff_listing',
                                'render_url' => $this->urlBuilder->getUrl('mui/index/render/'),
                                'update_url' => $this->urlBuilder->getUrl('test'),
                                'realTimeLink' => true,
                                'dataLinks' => [
                                    'imports' => false,
                                    'exports' => true
                                ],
                                'behaviourType' => 'simple',
                                'externalFilterMode' => true,
                                'id_product' => $this->locator->getProduct()->getId(),
                                'imports' => [
                                    'productId' => '${ $.provider }:data.current_product_id'
                                ],
                                'exports' => [
                                    'productId' => '${ $.externalProvider }:params.current_product_id'
                                ],
                                // 'imports' => [
                                //     'productId' => '${ $.provider }:data.product.current_product_id'
                                // ],
                                // 'exports' => [
                                //     'productId' => '${ $.externalProvider }:params.current_product_id'
                                // ],

                            ],
                        ],
                    ],
                ],
            ],
            'arguments' => [
                'data' => [
                    'config' => [
                        'label' => __('Staff Manager'),
                        'sortOrder' => 1,
                        'componentType' => Fieldset::NAME,
                        'collapsible' => true,
                        'sortOrder' =>
                        $this->getNextGroupSortOrder(
                            $meta,
                            static::GROUP_CONTENT,
                            static::SORT_ORDER
                        ),
                    ],
                ],
            ],
        ];

        return $meta;
    }
    public function modifyData(array $data){
        $productId = $this->locator->getProduct()->getId();
        $data[$productId][self::DATA_SOURCE_DEFAULT]['current_product_id'] = $productId;
        return $data;
    }
    private function getModuleManager()
    {
        if ($this->moduleManager === null) {
            $this->moduleManager = ObjectManager::getInstance()->get(ModuleManager::class);
        }
        return $this->moduleManager;
    }
}

2 Answers 2

0

In the function getData() in Ui/DataProvider/StaffDataProvider is called by Magento 2 times. The first call is made by GET to {{domain_admin}}/mui/index/render/?namespace=staff_listing&isAjax=true, in which only $this->request->getParams() the namespace variable and isAjax appear. And in the second call is made by POST and receives more parameters. Among them, the current_product_id

1
  • any solution found yet for this as i can't able to get current_product_id in post param.even it is not passed in the params.
    – Zahirabbas
    Jan 11, 2022 at 10:51
0

You missed some parts in your code

Ui\DataProvider\Product\Form\Modifier

uncomment this part

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

Ui/DataProvider/StaffDataProvider

public function getData(){
        // here you can get current_product_id
        $productId  = $this->request->getParam('current_product_id', 0);
 
        foreach ($this->pool->getModifiersInstances() as $modifier) {
            $this->data = $modifier->modifyData($this->data);
        }
        return $this->data;
    }

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.