I want to add my own custom option for an attribute.Please see attached image.
Just guide me which files i need to create.
I have just created one file for now. Please see code below, app/code/Vendor/Module/view/base/ui_component/customer_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="customer">
<field name="ffl" formElement="select">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">customer</item>
</item>
</argument>
<settings>
<dataType>text</dataType>
<visible>true</visible>
</settings>
</field>
</fieldset>
</form>
asked Feb 14, 2019 at 7:01
Sunny Rahevar
3,1721 gold badge25 silver badges46 bronze badges
-
This should be done by installdata or upgradedata to create an customer attribute with options as well.Prathap Gunasekaran– Prathap Gunasekaran2019年02月14日 07:08:24 +00:00Commented Feb 14, 2019 at 7:08
-
Is there any way to call option using tooptionArray() or something using model file or UI component file?Sunny Rahevar– Sunny Rahevar2019年02月14日 07:10:13 +00:00Commented Feb 14, 2019 at 7:10
1 Answer 1
Try this,
<?xml version="1.0" encoding="UTF-8"?><form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="customer">
<field name="custom_field" formElement="select">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">customer</item>
</item>
</argument>
<formElements>
<select>
<settings>
<options class="Vendor\Module\Model\Config\Source\Options"/>
</settings>
</select>
</formElements>
<settings>
<dataType>text</dataType>
<visible>true</visible>
</settings>
</field>
</fieldset>
In the options file, add the below code
<?php
namespace Vendor\Module\Model\Config\Source;
class Options implements \Magento\Framework\Option\ArrayInterface
{
/**
* Options for Type
*
* @return array
*/
public function toOptionArray()
{
return [
['value' => 1, 'label' => __('Type 1')],
['value' => 2, 'label' => __('Type 2')],
['value' => 3, 'label' => __('Type 3')]
];
}}
Hope this helps. Peace :)
answered Feb 14, 2019 at 7:34
Prathap Gunasekaran
3,2891 gold badge17 silver badges39 bronze badges
-
Yes,it works for me.I have little update in the XML file.Please check updated code in this link. ufile.io/gl2hg please update code and then i will accept this answer.Because it may be useful for other.Sunny Rahevar– Sunny Rahevar2019年02月14日 08:43:40 +00:00Commented Feb 14, 2019 at 8:43
-
Updated the answerPrathap Gunasekaran– Prathap Gunasekaran2019年02月14日 08:56:30 +00:00Commented Feb 14, 2019 at 8:56
-
Is there any way to Type 2 display as selected?Sunny Rahevar– Sunny Rahevar2019年02月14日 12:01:44 +00:00Commented Feb 14, 2019 at 12:01
-
Not sure public function toOptionArray() { $attributesArrays = array(); $attributesArrays[0] =array( 'label' => '0', 'value' => 'One' ); $attributesArrays[1] =array( 'label' => '1', 'value' => 'Two' ); $attributesArrays[2] =array( 'label' => '3', 'value' => 'Three' ); return $attributesArrays; }Prathap Gunasekaran– Prathap Gunasekaran2019年02月14日 12:09:51 +00:00Commented Feb 14, 2019 at 12:09
Explore related questions
See similar questions with these tags.
default