Site icon MageComp Blog

Magento 2: How to Add New Field in Admin User Create Form

Hello Magento Friends,

I am back with another Magento How-to solution. Today I will explain Magento 2: How to Add New Field in Admin User Create Form.

Customization is a fantastic feature provided by Magento. You can develop a fully custom-made store with Magento. Sometimes you need to save more information about admin users. To do that you need to create a custom field in the admin user create a form to save custom field information. Let’s learn How to Add New Field in Admin User Create Form in Magento 2.

Steps to Add New Field in Admin User Create Form in Magento 2:

Step 1: Firstly, create a “di.xml” file at the below path.

app\code\Vendor\Extension\adminhtml\etc\di.xml

Now add the code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\User\Block\User\Edit\Tab\Main">
        <plugin name="admin_user_image" type="Vendor\Extension\Plugin\Block\Adminhtml\User\Edit\Tab\UserField" sortOrder="1"/>
    </type>
</config>

Step 2: After that, we need to create a  “UserField.php” file at the below path

app\code\Vendor\Extension\Plugin\Block\Adminhtml\User\Edit\Tab\UserField.php

And add the below code

<?php 
namespace Vendor\Extension\Plugin\Block\Adminhtml\User\Edit\Tab;

class UserField
{
    public function aroundGetFormHtml(
        \Magento\User\Block\User\Edit\Tab\Main $subject,
        \Closure $proceed)
    {
        $form = $subject->getForm();
        if (is_object($form))
        {
            $fieldset = $form->addFieldset('admin_user_image', ['legend' => __('Custom Field')]);
            $fieldset->addField(
                'user_image',
                'image',
                [
                    'name' => 'user_image',
                    'label' => __('Image'),
                    'id' => 'user_image',
                    'title' => __('Image'),
                    'required' => false,
                    'note' => 'Allow image type: jpg, jpeg, png'
                ]
            );

            $subject->setForm($form);
        }

        return $proceed();
    }
}

Here, we have added an image custom field. However, you can add your desired custom field by customizing the above code.

Conclusion:

Hopefully, all are able to Add New Field in Admin User Create Form in Magento 2. In case of any difficulty, let me know via the comment section. Share the article with your Magento friends and via social media. Stay in touch with us.

Happy Coding!

Exit mobile version