I have a UI form in admin with store switcher. I need store_id in post data.
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
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>
- 
 what if i am using for new and edit action from same . so how can i set store id for new action?Rutvee Sojitra– Rutvee Sojitra2018年11月29日 12:57:05 +00:00Commented 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.Ramkishan Suthar– Ramkishan Suthar2018年11月30日 03:17:36 +00:00Commented 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 luckRutvee Sojitra– Rutvee Sojitra2018年11月30日 04:30:57 +00:00Commented 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()Juliano Vargas– Juliano Vargas2019年06月28日 16:05:59 +00:00Commented 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.Ramkishan Suthar– Ramkishan Suthar2019年06月29日 05:21:34 +00:00Commented Jun 29, 2019 at 5:21
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');
- 
 Thanks @milind for answer. I got the solution and posted it as answer.Ramkishan Suthar– Ramkishan Suthar2018年11月24日 12:16:59 +00:00Commented Nov 24, 2018 at 12:16
Explore related questions
See similar questions with these tags.