Question

I have a UI form in admin with store switcher. I need store_id in post data.

enter image description here

As all can see store_id in url when I change store from store switcher but once I submit it I am not getting any store value in post data. So I think I need to set store value in a hidden field. I don't know I am on right track or not. Please help guys if anyone faced this issue before.

Was it helpful?

Solution

I just need to set store id in DataProvider file like below.

<?php
namespace Vendor\Module\Model\Providers;
use Vendor\Module\Model\ResourceModel\Company\CollectionFactory;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    /**
     * @var array
     */
    protected $loadedData;

    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $taskCollectionFactory
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $companyCollectionFactory,
        \Magento\Framework\App\Request\Http $request, 
        \Magento\Store\Api\StoreRepositoryInterface $storeRepository,        
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $companyCollectionFactory->create();
        $this->request = $request;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();
        foreach ($items as $item) { 
            $item->setStoreId($this->request->getParam('store'));
            $this->loadedData[$item->getEntityId()] = $item->getData();
        } 

        return $this->loadedData;
    }
}

ui_form.xml

<field name="store_id" sortOrder="65" formElement="hidden">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">company</item>
            </item>
        </argument>
        <settings>
            <dataType>hidden</dataType>
            <dataScope>store_id</dataScope>
        </settings>
</field>

OTHER TIPS

Kindly use getting all get, post, and url values in the controller action:

        /** @var \Magento\Backend\App\Action $this*/
        /** @var array $params*/
        $params = $this->getRequest()->getParams();

You can also get each value by:

         /** @var string $store*/
        $store = $this->getRequest()->getParam('store');
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top