I've searched everywhere but I'm unable to find a field that can be inserted into a UI Component form that allows the user to select a product.
It can be achieved easily for categories with the following code:
<field name="pick_a_category">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Magento\Catalog\Ui\Component\Product\Form\Categories\Options</item>
<item name="config" xsi:type="array">
...
</item>
</item>
</argument>
</field>
Is there an equivalent approach for products?
Best Answer
I was working on this myself, and came up with the following workaround.
I've reused a portion of the parent category selector on the category edit form: Magento_Catalog/view/adminhtml/ui_component/new_category_form.xml (field "parent").
In your ui_component XML file:
In your Vendor\Module\Model\Source\Products class:
In your Save controller, check the post object for the presence of the parameter:
Lastly, in your DataProvider, ensure the entity's current data is loaded on page load:
The ["123","456"] array is where you stipulate the product ID's of course. In my case, my products column in the entity's table is already a comma separated string (check the Save controller above where I do this). I can simply explode this into an array. I load the entity by the ID and grab whatever the current value is, such as below:
I can then assign that as the products data variable:
Here's how it looks and works. NB: the search works based on the label we stipulated in the Vendor\Module\Model\Source\Products
toOptionArray
method: