1define([
2 'Magento_Ui/js/form/element/ui-select'
3], function (Select) {
4 'use strict';
5 return Select.extend({
6 /**
7 * Parse data and set it to options.
8 *
9 * @param {Object} data - Response data object.
10 * @returns {Object}
11 */
12 setParsed: function (data) {
13 var option = this.parseData(data);
14 if (data.error) {
15 return this;
16 }
17 this.options([]);
18 this.setOption(option);
19 this.set('newOption', option);
20 },
21 /**
22 * Normalize option object.
23 *
24 * @param {Object} data - Option object.
25 * @returns {Object}
26 */
27 parseData: function (data) {
28 return {
29 value: data.customer.entity_id,
30 label: data.customer.name
31 };
32 }
33 });
34});
35
1<?php
2namespace Webkul\Test\Ui\Component\Create\Form\Customer;
3
4use Magento\Framework\Data\OptionSourceInterface;
5use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
6use Magento\Framework\App\RequestInterface;
7
8/**
9 * Options tree for "Categories" field
10 */
11class Options implements OptionSourceInterface
12{
13 /**
14 * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory
15 */
16 protected $customerCollectionFactory;
17
18 /**
19 * @var RequestInterface
20 */
21 protected $request;
22
23 /**
24 * @var array
25 */
26 protected $customerTree;
27
28 /**
29 * @param CustomerCollectionFactory $customerCollectionFactory
30 * @param RequestInterface $request
31 */
32 public function __construct(
33 CustomerCollectionFactory $customerCollectionFactory,
34 RequestInterface $request
35 ) {
36 $this->customerCollectionFactory = $customerCollectionFactory;
37 $this->request = $request;
38 }
39
40 /**
41 * {@inheritdoc}
42 */
43 public function toOptionArray()
44 {
45 return $this->getCustomerTree();
46 }
47
48 /**
49 * Retrieve categories tree
50 *
51 * @return array
52 */
53 protected function getCustomerTree()
54 {
55 if ($this->customerTree === null) {
56 $collection = $this->customerCollectionFactory->create();
57
58 $collection->addNameToSelect();
59
60 foreach ($collection as $customer) {
61 $customerId = $customer->getEntityId();
62 if (!isset($customerById[$customerId])) {
63 $customerById[$customerId] = [
64 'value' => $customerId
65 ];
66 }
67 $customerById[$customerId]['label'] = $customer->getName();
68 }
69 $this->customerTree = $customerById;
70 }
71 return $this->customerTree;
72 }
73}
74
1<field name="customer" component="Webkul_Test/js/components/select-customer" sortOrder="20" formElement="select">
2 <argument name="data" xsi:type="array">
3 <item name="config" xsi:type="array">
4 <item name="filterOptions" xsi:type="boolean">true</item>//to add filter in select-ui
5 <item name="multiple" xsi:type="boolean">false</item>//select multiple or not
6 <item name="showCheckbox" xsi:type="boolean">true</item>
7 <item name="disableLabel" xsi:type="boolean">true</item>
8 </item>
9 </argument>
10 <settings>
11 <required>true</required>
12 <validation>
13 <rule name="required-entry" xsi:type="boolean">true</rule>
14 </validation>
15 <elementTmpl>ui/grid/filters/elements/ui-select</elementTmpl>
16 <label translate="true">Select Customer</label>//label to Field
17 <dataScope>data.customer</dataScope>//To map
18 <componentType>field</componentType>
19 <listens>
20 <link name="${ $.namespace }.${ $.namespace }:responseData">setParsed</link>
21 </listens>
22 </settings>
23 <formElements>
24 <select>
25 <settings>
26 <options class="Webkul\Test\Ui\Component\Create\Form\Customer\Options"/>
27 </settings>
28 </select>
29 </formElements>
30</field>
31