5

I photoshoped what I want:

enter image description here

I create a repo for that extension, so you can see all the code:

https://github.com/slayerbleast/CustomerAttributes

What I did:

I added some customerAttributes: InstallData.php

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customAttributes = [
        'test1',
        'test2',
        'test3',
        'test4',
        'test5',
    ];

    /**
     * Clean attributes
     */
    foreach ($customAttributes as $customAttribute) {
        $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, $customAttribute);
    }

    $customerSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'test1',
        [
            'type' => 'varchar',
            'label' => __('Test 1'),
            'input' => 'text',
            'required' => 0,
            'visible' => 1,
            'user_defined' => 0,
            'system' => 0,
            'position' => 500
        ]
    );

    $customerSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'test2',
        [
            'type' => 'varchar',
            'label' => __('Test 2'),
            'input' => 'text',
            'required' => 0,
            'visible' => 1,
            'user_defined' => 0,
            'system' => 0,
            'position' => 510
        ]
    );

    $customerSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'test3',
        [
            'type' => 'varchar',
            'label' => __('Test 3'),
            'input' => 'text',
            'required' => 0,
            'visible' => 1,
            'user_defined' => 0,
            'system' => 0,
            'position' => 520
        ]
    );

    $customerSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'test4',
        [
            'type' => 'varchar',
            'label' => __('Test 4'),
            'input' => 'text',
            'required' => 0,
            'visible' => 1,
            'user_defined' => 0,
            'system' => 0,
            'position' => 530
        ]
    );

    $customerSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'test5',
        [
            'type' => 'varchar',
            'label' => __('Test 5'),
            'input' => 'text',
            'required' => 0,
            'visible' => 1,
            'user_defined' => 0,
            'system' => 0,
            'position' => 540
        ]
    );


    foreach ($customAttributes as $customAttribute) {
        $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $customAttribute)
            ->setData('used_in_forms', ['adminhtml_customer'])
            ->save();
    }


    $groupName = 'Aditional'; /* Label of your group*/
    $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY); /* get entity type id so that attribute are only assigned to customer  */
    $attributeSetIds = $customerSetup->getAllAttributeSetIds($entityTypeId); /* Here we have fetched all attribute set as we want attribute group to show under all attribute set.*/

    foreach ($attributeSetIds as $attributeSetId) {
        $customerSetup->addAttributeGroup($entityTypeId, $attributeSetId, $groupName, 99);
        $attributeGroupId = $customerSetup->getAttributeGroupId($entityTypeId, $attributeSetId, $groupName);

        // Add existing attribute to group
        foreach ($customAttributes as $customAttribute) {
            $attributeId = $customerSetup->getAttributeId($entityTypeId, $customAttribute);
            $customerSetup->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, $attributeId, null);
        }
    }
}

I see the InstallData script as a 3 parts:

  1. Define the attributes.
  2. Define in what forms are used.
  3. Define groups (I expect groups are what I want, it will add a legend for that group)

But it's not working. Magento only creates automatically my fields on the order I defined. I tried everything and I don't know how to do it.

I tried defining my customer_form.xml ui and add a fieldset into another fieldset, and visually was okay but then all the functionality was lost.

I can't believe this silly thing I need to do is so much complicated. I only need to add a legend named "Additional" before my fields...

1
  • I have edited my answer.. you can check ... and let me know if found any issue
    – Ashish Raj
    Dec 11, 2017 at 13:15

1 Answer 1

1

Yes right if you have added attribute by InstallData then fields will be created automatically without adding XML. So for add legends between fields on customer form adminhtml create a attribute group like below code for product attribute group and assign your attribute to them:

$groupName = 'Aditional Fields'; /* Label of your group*/
$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY); /* get entity type id so that attribute are only assigned to customer  */
$attributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId); /* Here we have fetched all attribute set as we want attribute group to show under all attribute set.*/

foreach($attributeSetIds as $attributeSetId) {
    $eavSetup->addAttributeGroup($entityTypeId, $attributeSetId, $groupName, 19);
    $attributeGroupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, $groupName);
    // Add existing attribute to group
    $attributeId = $eavSetup->getAttributeId($entityTypeId, 'gsp_groupnum');
    $eavSetup->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, $attributeId, null);
}

Below code is in case you are adding custom fields to customer form which are not attribute but custom field of a custom table.

You need to add a fieldset inside existing fieldset before/above the field on which you want add legend like below.

<fieldset name="customer">
    <button name="test">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">button</item>
                <item name="formElement" xsi:type="string">button</item>
                <item name="source" xsi:type="string">customer</item>
                <item name="sortOrder" xsi:type="number">500</item>
                <item name="label" xsi:type="string" translate="true">test</item>
            </item>
        </argument>
    </button>
<!-- below code for add legends between fields -->
    <fieldset name="additional_fields">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Aditional Fields</item>
                <item name="collapsible" xsi:type="boolean">false</item>
        </argument>
        <field name="groupnum">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">number</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">customer</item>
                    <item name="sortOrder" xsi:type="number">510</item>
                    <item name="label" xsi:type="string" translate="true">Payment terms</item>
                </item>
            </argument>
        </field>
        <field name="paymethcod">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">number</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">customer</item>
                    <item name="sortOrder" xsi:type="number">520</item>
                    <item name="label" xsi:type="string" translate="true">Way of payment</item>
                </item>
            </argument>
        </field>
    </fieldset>
</fieldset>
7
  • I updated the question, still not working :(
    – Marc Pont
    Dec 11, 2017 at 9:43
  • I did what you suggested, creating this group but I really don't know what I'm doing... The group is created, but now what? I only labeled the group but the id additional_fields it is not defined. Besides, I changed the entityType for customer I'm working with customer attributes not product attributes.
    – Marc Pont
    Dec 11, 2017 at 15:18
  • I have suggested a way/example... yes that is for product but you can convert it for customer as per your need.
    – Ashish Raj
    Dec 12, 2017 at 5:34
  • I have converted it to customer attribute group.. you can review
    – Ashish Raj
    Dec 12, 2017 at 7:35
  • I updated the question, I followed your suggestions, but I'm missing something. Thank you for helping me.
    – Marc Pont
    Dec 12, 2017 at 9:49

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.