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

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

6 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago