2

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.

2 Answers 2

6

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>
5
  • what if i am using for new and edit action from same . so how can i set store id for new action? Nov 29, 2018 at 12:57
  • 1
    @RutveeSojitra Actually on add form there will be no option of store switcher so it will save as store_id 0 while add. I got inspired from product add/edit form. Nov 30, 2018 at 3:17
  • Thanks ramkishan . Actually i am migrating magento1 to magento2 and in my magento1 there is functionality to save store id when we add so am finding same for magento2. But no luck Nov 30, 2018 at 4:30
  • @Ramkishan Suthar What If the form is empty that means is New how to populate the field? as $this->loadedData returns Array() Jun 28, 2019 at 16:05
  • @JulianoVargas while adding a new entry store_id will be saved as 0. I checked in default product add/edit form. Jun 29, 2019 at 5:21
1

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');
1
  • Thanks @milind for answer. I got the solution and posted it as answer. Nov 24, 2018 at 12:16

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.