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
1 2 3 4 5 6 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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!
above method not rendering the fields data on edit
The blog is self for the create user form, so it will not added into the edit.
Hey, why we have used around plugin instead of after/before plugin
Use around plugin because we need to add field with other fields as well.
How can i add a fieldset of image or file type which allows to select and upload multiple images at once ?
You can add image field type using “imageUploader”