6

I want to add my own custom option for an attribute.Please see attached image.

enter image description here

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>
2
  • This should be done by installdata or upgradedata to create an customer attribute with options as well. Commented Feb 14, 2019 at 7:08
  • Is there any way to call option using tooptionArray() or something using model file or UI component file? Commented Feb 14, 2019 at 7:10

1 Answer 1

2

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 :)

4
  • 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. Commented Feb 14, 2019 at 8:43
  • Updated the answer Commented Feb 14, 2019 at 8:56
  • Is there any way to Type 2 display as selected? Commented 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; } Commented Feb 14, 2019 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.