How To

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!

Click to rate this post!
[Total: 8 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago