Question

I have a UI grid form in the admin, that allows the admin to edit the data in a grid. The grid is listing data from a custom table multivendor_pickupinstore with structure below:

id
seller_id
order_id
product_type
pickup_date
created_at
status

Now in the edit form I need to add the drop-down to choose the seller. The data is in another table multivendor_user with columns as below:

is_vendor
userstatus
user_id
priority
storeurl
storetitle
email
name  

What I have done so far is create the form with all the input fields. please see the field component in the form.xml file,

    <!-- This field has data type 'select' and standard 'input' form element and looks like input -->
    <field name="seller_id">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">[Vendor]\[Module]\Model\Select\Source\Options</item>
            <item name="config" xsi:type="array">
                <item name="componentType" xsi:type="string">field</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="label" xsi:type="string">Seller</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="source" xsi:type="string">p_pickups</item>
            </item>
        </argument>
    </field>  

I understand that the model [Vendor]\[Module]\Model\Select\Source\Options can be used to return the required option array. I have the model [Vendor2]\[Module2]\Model\Sellers that can be used to return the seller collection (table multivendor_user). But I am not able to get data from custom table. Could someone please help me with this. Thank you in advance.

Was it helpful?

Solution

After trying few methods I managed to get the options as below:

namespace [Vendor]\[Module]\Model\Select\Source;
use Magento\Framework\Option\ArrayInterface;
class Options implements ArrayInterface
{
    protected $options;
    public function __construct(
        [Vendor2]\[Module2]\Model\SellersFactory $sellersFactory
    ) {
        $this->sellersFactory = $sellersFactory;
    }
    public function toOptionArray()
    {
        $sellerModel = $this->sellersFactory->create()->getCollection()
                                           ->addFieldToSelect( array('user_id','storetitle','name') );
        $options = array( 'label' => 'Please select', 'value' => ''  );
        if( $sellerModel->getSize() ){
            foreach ( $sellerModel  as $seller) {
                $options[] = array( 
                'label' => ucfirst( $seller->getData('storetitle') ) ,
                'value' => $seller->getData('user_id')
                );
            }
        }
        return $options;
    }
}  

Not sure if this is the best method though. Please share if there is a better way. Thank you

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top