Magento 2 Add new field to Magento_User admin form












8















I am looking for a good approach with add/update already prepared (by default) Magento User's (module-user) form. The form can be reached in admin panel by this path:



System > All users > [chosen_user] > User's main edit tab (Account Information)



Now I'm trying with using di.xml in my custom module where I specify dependencies:
`



<preference for="MagentoUserBlockUserEditTabMain" type="Vendor_NameModule_NameBlockUserEditTabMain" />
<preference for="MagentoUserBlockRoleGridUser" type="Vendor_NameModule_NameBlockRoleGridUser" />


`



This is content that I've already made for a Main.php class





// @codingStandardsIgnoreFile

namespace Vendor_NameModule_NameBlockUserEditTab;

use MagentoUserBlockUserEditTabMain as UserEditMainTab;
use MagentoBackendBlockTemplateContext;
use MagentoFrameworkRegistry;
use MagentoFrameworkDataFormFactory;
use MagentoBackendModelAuthSession;
use MagentoFrameworkLocaleListsInterface;

class Main extends UserEditMainTab
{
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
Session $authSession,
ListsInterface $localeLists,
array $data =
) {
parent::__construct($context, $registry, $formFactory, $authSession, $localeLists, $data);
}

protected function _prepareForm()
{
/** @var $model MagentoUserModelUser */
$model = $this->_coreRegistry->registry('permissions_user');

/** @var MagentoFrameworkDataForm $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('user_');

$baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information __ TEST')]);

if ($model->getUserId()) {
$baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
} else {
if (!$model->hasData('is_active')) {
$model->setIsActive(1);
}
}

$baseFieldset->addField(
'user_image',
'image',
[
'name' => 'user_image',
'label' => __('User Image'),
'id' => 'user_image',
'title' => __('User Image'),
'required' => false,
'note' => 'Allow image type: jpg, jpeg, png'
]
);

$baseFieldset->addField(
'username',
'text',
[
'name' => 'username',
'label' => __('User Name'),
'id' => 'username',
'title' => __('User Name'),
'required' => true
]
);

$baseFieldset->addField(
'firstname',
'text',
[
'name' => 'firstname',
'label' => __('First Name'),
'id' => 'firstname',
'title' => __('First Name'),
'required' => true
]
);

$baseFieldset->addField(
'lastname',
'text',
[
'name' => 'lastname',
'label' => __('Last Name'),
'id' => 'lastname',
'title' => __('Last Name'),
'required' => true
]
);

$baseFieldset->addField(
'email',
'text',
[
'name' => 'email',
'label' => __('Email'),
'id' => 'customer_email',
'title' => __('User Email'),
'class' => 'required-entry validate-email',
'required' => true
]
);

$isNewObject = $model->isObjectNew();
if ($isNewObject) {
$passwordLabel = __('Password');
} else {
$passwordLabel = __('New Password');
}
$confirmationLabel = __('Password Confirmation');
$this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);

$baseFieldset->addField(
'interface_locale',
'select',
[
'name' => 'interface_locale',
'label' => __('Interface Locale'),
'title' => __('Interface Locale'),
'values' => $this->_LocaleLists->getTranslatedOptionLocales(),
'class' => 'select'
]
);

if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
$baseFieldset->addField(
'is_active',
'select',
[
'name' => 'is_active',
'label' => __('This account is'),
'id' => 'is_active',
'title' => __('Account Status'),
'class' => 'input-select',
'options' => ['1' => __('Active'), '0' => __('Inactive')]
]
);
}

$baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

$currentUserVerificationFieldset = $form->addFieldset(
'current_user_verification_fieldset',
['legend' => __('Current User Identity Verification')]
);
$currentUserVerificationFieldset->addField(
self::CURRENT_USER_PASSWORD_FIELD,
'password',
[
'name' => self::CURRENT_USER_PASSWORD_FIELD,
'label' => __('Your Password'),
'id' => self::CURRENT_USER_PASSWORD_FIELD,
'title' => __('Your Password'),
'class' => 'input-text validate-current-password required-entry',
'required' => true
]
);

$data = $model->getData();
unset($data['password']);
unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
$form->setValues($data);

$this->setForm($form);

return parent::_prepareForm();
}
}


and some code for User.php




namespace Vendor_NameModule_NameBlockRoleGrid;

use MagentoUserBlockRoleGridUser as RoleGridUser;
use MagentoBackendBlockWidgetGridExtended as ExtendedGrid;

class User extends RoleGridUser
{
protected function _prepareColumns()
{
parent::_prepareCollection();

$this->addColumn(
'user_image',
[
'header' => __('User Image'),
'width' => 5,
'align' => 'left',
'sortable' => true,
'index' => 'user_image'
]
);

return ExtendedGrid::_prepareCollection();
}
}


If you take a look closer you already now that I'm trying to add a field with user's image.



Unfortunately, I don't see any changes in admin front. Of course, the needed column was added by InstallSchema script earlier to 'admin_user' table.



Contents of directories in a tree-like format:




Module_Name
├── Block
│   ├── Catalog
│   │   └── Product
│   │   └── RelatedPosts.php
│   ├── Role
│   │   └── Grid
│   │   └── User.php
│   └── User
│   └── Edit
│   └── Tab
│   └── Main.php
├── composer.json
├── etc
│   ├── di.xml
│   └── module.xml
├── Setup
   └── InstallSchema.php


What Did I do Wrong?










share|improve this question

























  • The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

    – Great Indian Brain
    Jul 25 '17 at 10:58
















8















I am looking for a good approach with add/update already prepared (by default) Magento User's (module-user) form. The form can be reached in admin panel by this path:



System > All users > [chosen_user] > User's main edit tab (Account Information)



Now I'm trying with using di.xml in my custom module where I specify dependencies:
`



<preference for="MagentoUserBlockUserEditTabMain" type="Vendor_NameModule_NameBlockUserEditTabMain" />
<preference for="MagentoUserBlockRoleGridUser" type="Vendor_NameModule_NameBlockRoleGridUser" />


`



This is content that I've already made for a Main.php class





// @codingStandardsIgnoreFile

namespace Vendor_NameModule_NameBlockUserEditTab;

use MagentoUserBlockUserEditTabMain as UserEditMainTab;
use MagentoBackendBlockTemplateContext;
use MagentoFrameworkRegistry;
use MagentoFrameworkDataFormFactory;
use MagentoBackendModelAuthSession;
use MagentoFrameworkLocaleListsInterface;

class Main extends UserEditMainTab
{
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
Session $authSession,
ListsInterface $localeLists,
array $data =
) {
parent::__construct($context, $registry, $formFactory, $authSession, $localeLists, $data);
}

protected function _prepareForm()
{
/** @var $model MagentoUserModelUser */
$model = $this->_coreRegistry->registry('permissions_user');

/** @var MagentoFrameworkDataForm $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('user_');

$baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information __ TEST')]);

if ($model->getUserId()) {
$baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
} else {
if (!$model->hasData('is_active')) {
$model->setIsActive(1);
}
}

$baseFieldset->addField(
'user_image',
'image',
[
'name' => 'user_image',
'label' => __('User Image'),
'id' => 'user_image',
'title' => __('User Image'),
'required' => false,
'note' => 'Allow image type: jpg, jpeg, png'
]
);

$baseFieldset->addField(
'username',
'text',
[
'name' => 'username',
'label' => __('User Name'),
'id' => 'username',
'title' => __('User Name'),
'required' => true
]
);

$baseFieldset->addField(
'firstname',
'text',
[
'name' => 'firstname',
'label' => __('First Name'),
'id' => 'firstname',
'title' => __('First Name'),
'required' => true
]
);

$baseFieldset->addField(
'lastname',
'text',
[
'name' => 'lastname',
'label' => __('Last Name'),
'id' => 'lastname',
'title' => __('Last Name'),
'required' => true
]
);

$baseFieldset->addField(
'email',
'text',
[
'name' => 'email',
'label' => __('Email'),
'id' => 'customer_email',
'title' => __('User Email'),
'class' => 'required-entry validate-email',
'required' => true
]
);

$isNewObject = $model->isObjectNew();
if ($isNewObject) {
$passwordLabel = __('Password');
} else {
$passwordLabel = __('New Password');
}
$confirmationLabel = __('Password Confirmation');
$this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);

$baseFieldset->addField(
'interface_locale',
'select',
[
'name' => 'interface_locale',
'label' => __('Interface Locale'),
'title' => __('Interface Locale'),
'values' => $this->_LocaleLists->getTranslatedOptionLocales(),
'class' => 'select'
]
);

if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
$baseFieldset->addField(
'is_active',
'select',
[
'name' => 'is_active',
'label' => __('This account is'),
'id' => 'is_active',
'title' => __('Account Status'),
'class' => 'input-select',
'options' => ['1' => __('Active'), '0' => __('Inactive')]
]
);
}

$baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

$currentUserVerificationFieldset = $form->addFieldset(
'current_user_verification_fieldset',
['legend' => __('Current User Identity Verification')]
);
$currentUserVerificationFieldset->addField(
self::CURRENT_USER_PASSWORD_FIELD,
'password',
[
'name' => self::CURRENT_USER_PASSWORD_FIELD,
'label' => __('Your Password'),
'id' => self::CURRENT_USER_PASSWORD_FIELD,
'title' => __('Your Password'),
'class' => 'input-text validate-current-password required-entry',
'required' => true
]
);

$data = $model->getData();
unset($data['password']);
unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
$form->setValues($data);

$this->setForm($form);

return parent::_prepareForm();
}
}


and some code for User.php




namespace Vendor_NameModule_NameBlockRoleGrid;

use MagentoUserBlockRoleGridUser as RoleGridUser;
use MagentoBackendBlockWidgetGridExtended as ExtendedGrid;

class User extends RoleGridUser
{
protected function _prepareColumns()
{
parent::_prepareCollection();

$this->addColumn(
'user_image',
[
'header' => __('User Image'),
'width' => 5,
'align' => 'left',
'sortable' => true,
'index' => 'user_image'
]
);

return ExtendedGrid::_prepareCollection();
}
}


If you take a look closer you already now that I'm trying to add a field with user's image.



Unfortunately, I don't see any changes in admin front. Of course, the needed column was added by InstallSchema script earlier to 'admin_user' table.



Contents of directories in a tree-like format:




Module_Name
├── Block
│   ├── Catalog
│   │   └── Product
│   │   └── RelatedPosts.php
│   ├── Role
│   │   └── Grid
│   │   └── User.php
│   └── User
│   └── Edit
│   └── Tab
│   └── Main.php
├── composer.json
├── etc
│   ├── di.xml
│   └── module.xml
├── Setup
   └── InstallSchema.php


What Did I do Wrong?










share|improve this question

























  • The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

    – Great Indian Brain
    Jul 25 '17 at 10:58














8












8








8


2






I am looking for a good approach with add/update already prepared (by default) Magento User's (module-user) form. The form can be reached in admin panel by this path:



System > All users > [chosen_user] > User's main edit tab (Account Information)



Now I'm trying with using di.xml in my custom module where I specify dependencies:
`



<preference for="MagentoUserBlockUserEditTabMain" type="Vendor_NameModule_NameBlockUserEditTabMain" />
<preference for="MagentoUserBlockRoleGridUser" type="Vendor_NameModule_NameBlockRoleGridUser" />


`



This is content that I've already made for a Main.php class





// @codingStandardsIgnoreFile

namespace Vendor_NameModule_NameBlockUserEditTab;

use MagentoUserBlockUserEditTabMain as UserEditMainTab;
use MagentoBackendBlockTemplateContext;
use MagentoFrameworkRegistry;
use MagentoFrameworkDataFormFactory;
use MagentoBackendModelAuthSession;
use MagentoFrameworkLocaleListsInterface;

class Main extends UserEditMainTab
{
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
Session $authSession,
ListsInterface $localeLists,
array $data =
) {
parent::__construct($context, $registry, $formFactory, $authSession, $localeLists, $data);
}

protected function _prepareForm()
{
/** @var $model MagentoUserModelUser */
$model = $this->_coreRegistry->registry('permissions_user');

/** @var MagentoFrameworkDataForm $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('user_');

$baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information __ TEST')]);

if ($model->getUserId()) {
$baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
} else {
if (!$model->hasData('is_active')) {
$model->setIsActive(1);
}
}

$baseFieldset->addField(
'user_image',
'image',
[
'name' => 'user_image',
'label' => __('User Image'),
'id' => 'user_image',
'title' => __('User Image'),
'required' => false,
'note' => 'Allow image type: jpg, jpeg, png'
]
);

$baseFieldset->addField(
'username',
'text',
[
'name' => 'username',
'label' => __('User Name'),
'id' => 'username',
'title' => __('User Name'),
'required' => true
]
);

$baseFieldset->addField(
'firstname',
'text',
[
'name' => 'firstname',
'label' => __('First Name'),
'id' => 'firstname',
'title' => __('First Name'),
'required' => true
]
);

$baseFieldset->addField(
'lastname',
'text',
[
'name' => 'lastname',
'label' => __('Last Name'),
'id' => 'lastname',
'title' => __('Last Name'),
'required' => true
]
);

$baseFieldset->addField(
'email',
'text',
[
'name' => 'email',
'label' => __('Email'),
'id' => 'customer_email',
'title' => __('User Email'),
'class' => 'required-entry validate-email',
'required' => true
]
);

$isNewObject = $model->isObjectNew();
if ($isNewObject) {
$passwordLabel = __('Password');
} else {
$passwordLabel = __('New Password');
}
$confirmationLabel = __('Password Confirmation');
$this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);

$baseFieldset->addField(
'interface_locale',
'select',
[
'name' => 'interface_locale',
'label' => __('Interface Locale'),
'title' => __('Interface Locale'),
'values' => $this->_LocaleLists->getTranslatedOptionLocales(),
'class' => 'select'
]
);

if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
$baseFieldset->addField(
'is_active',
'select',
[
'name' => 'is_active',
'label' => __('This account is'),
'id' => 'is_active',
'title' => __('Account Status'),
'class' => 'input-select',
'options' => ['1' => __('Active'), '0' => __('Inactive')]
]
);
}

$baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

$currentUserVerificationFieldset = $form->addFieldset(
'current_user_verification_fieldset',
['legend' => __('Current User Identity Verification')]
);
$currentUserVerificationFieldset->addField(
self::CURRENT_USER_PASSWORD_FIELD,
'password',
[
'name' => self::CURRENT_USER_PASSWORD_FIELD,
'label' => __('Your Password'),
'id' => self::CURRENT_USER_PASSWORD_FIELD,
'title' => __('Your Password'),
'class' => 'input-text validate-current-password required-entry',
'required' => true
]
);

$data = $model->getData();
unset($data['password']);
unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
$form->setValues($data);

$this->setForm($form);

return parent::_prepareForm();
}
}


and some code for User.php




namespace Vendor_NameModule_NameBlockRoleGrid;

use MagentoUserBlockRoleGridUser as RoleGridUser;
use MagentoBackendBlockWidgetGridExtended as ExtendedGrid;

class User extends RoleGridUser
{
protected function _prepareColumns()
{
parent::_prepareCollection();

$this->addColumn(
'user_image',
[
'header' => __('User Image'),
'width' => 5,
'align' => 'left',
'sortable' => true,
'index' => 'user_image'
]
);

return ExtendedGrid::_prepareCollection();
}
}


If you take a look closer you already now that I'm trying to add a field with user's image.



Unfortunately, I don't see any changes in admin front. Of course, the needed column was added by InstallSchema script earlier to 'admin_user' table.



Contents of directories in a tree-like format:




Module_Name
├── Block
│   ├── Catalog
│   │   └── Product
│   │   └── RelatedPosts.php
│   ├── Role
│   │   └── Grid
│   │   └── User.php
│   └── User
│   └── Edit
│   └── Tab
│   └── Main.php
├── composer.json
├── etc
│   ├── di.xml
│   └── module.xml
├── Setup
   └── InstallSchema.php


What Did I do Wrong?










share|improve this question
















I am looking for a good approach with add/update already prepared (by default) Magento User's (module-user) form. The form can be reached in admin panel by this path:



System > All users > [chosen_user] > User's main edit tab (Account Information)



Now I'm trying with using di.xml in my custom module where I specify dependencies:
`



<preference for="MagentoUserBlockUserEditTabMain" type="Vendor_NameModule_NameBlockUserEditTabMain" />
<preference for="MagentoUserBlockRoleGridUser" type="Vendor_NameModule_NameBlockRoleGridUser" />


`



This is content that I've already made for a Main.php class





// @codingStandardsIgnoreFile

namespace Vendor_NameModule_NameBlockUserEditTab;

use MagentoUserBlockUserEditTabMain as UserEditMainTab;
use MagentoBackendBlockTemplateContext;
use MagentoFrameworkRegistry;
use MagentoFrameworkDataFormFactory;
use MagentoBackendModelAuthSession;
use MagentoFrameworkLocaleListsInterface;

class Main extends UserEditMainTab
{
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
Session $authSession,
ListsInterface $localeLists,
array $data =
) {
parent::__construct($context, $registry, $formFactory, $authSession, $localeLists, $data);
}

protected function _prepareForm()
{
/** @var $model MagentoUserModelUser */
$model = $this->_coreRegistry->registry('permissions_user');

/** @var MagentoFrameworkDataForm $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('user_');

$baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information __ TEST')]);

if ($model->getUserId()) {
$baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
} else {
if (!$model->hasData('is_active')) {
$model->setIsActive(1);
}
}

$baseFieldset->addField(
'user_image',
'image',
[
'name' => 'user_image',
'label' => __('User Image'),
'id' => 'user_image',
'title' => __('User Image'),
'required' => false,
'note' => 'Allow image type: jpg, jpeg, png'
]
);

$baseFieldset->addField(
'username',
'text',
[
'name' => 'username',
'label' => __('User Name'),
'id' => 'username',
'title' => __('User Name'),
'required' => true
]
);

$baseFieldset->addField(
'firstname',
'text',
[
'name' => 'firstname',
'label' => __('First Name'),
'id' => 'firstname',
'title' => __('First Name'),
'required' => true
]
);

$baseFieldset->addField(
'lastname',
'text',
[
'name' => 'lastname',
'label' => __('Last Name'),
'id' => 'lastname',
'title' => __('Last Name'),
'required' => true
]
);

$baseFieldset->addField(
'email',
'text',
[
'name' => 'email',
'label' => __('Email'),
'id' => 'customer_email',
'title' => __('User Email'),
'class' => 'required-entry validate-email',
'required' => true
]
);

$isNewObject = $model->isObjectNew();
if ($isNewObject) {
$passwordLabel = __('Password');
} else {
$passwordLabel = __('New Password');
}
$confirmationLabel = __('Password Confirmation');
$this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);

$baseFieldset->addField(
'interface_locale',
'select',
[
'name' => 'interface_locale',
'label' => __('Interface Locale'),
'title' => __('Interface Locale'),
'values' => $this->_LocaleLists->getTranslatedOptionLocales(),
'class' => 'select'
]
);

if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
$baseFieldset->addField(
'is_active',
'select',
[
'name' => 'is_active',
'label' => __('This account is'),
'id' => 'is_active',
'title' => __('Account Status'),
'class' => 'input-select',
'options' => ['1' => __('Active'), '0' => __('Inactive')]
]
);
}

$baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

$currentUserVerificationFieldset = $form->addFieldset(
'current_user_verification_fieldset',
['legend' => __('Current User Identity Verification')]
);
$currentUserVerificationFieldset->addField(
self::CURRENT_USER_PASSWORD_FIELD,
'password',
[
'name' => self::CURRENT_USER_PASSWORD_FIELD,
'label' => __('Your Password'),
'id' => self::CURRENT_USER_PASSWORD_FIELD,
'title' => __('Your Password'),
'class' => 'input-text validate-current-password required-entry',
'required' => true
]
);

$data = $model->getData();
unset($data['password']);
unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
$form->setValues($data);

$this->setForm($form);

return parent::_prepareForm();
}
}


and some code for User.php




namespace Vendor_NameModule_NameBlockRoleGrid;

use MagentoUserBlockRoleGridUser as RoleGridUser;
use MagentoBackendBlockWidgetGridExtended as ExtendedGrid;

class User extends RoleGridUser
{
protected function _prepareColumns()
{
parent::_prepareCollection();

$this->addColumn(
'user_image',
[
'header' => __('User Image'),
'width' => 5,
'align' => 'left',
'sortable' => true,
'index' => 'user_image'
]
);

return ExtendedGrid::_prepareCollection();
}
}


If you take a look closer you already now that I'm trying to add a field with user's image.



Unfortunately, I don't see any changes in admin front. Of course, the needed column was added by InstallSchema script earlier to 'admin_user' table.



Contents of directories in a tree-like format:




Module_Name
├── Block
│   ├── Catalog
│   │   └── Product
│   │   └── RelatedPosts.php
│   ├── Role
│   │   └── Grid
│   │   └── User.php
│   └── User
│   └── Edit
│   └── Tab
│   └── Main.php
├── composer.json
├── etc
│   ├── di.xml
│   └── module.xml
├── Setup
   └── InstallSchema.php


What Did I do Wrong?







magento2 adminform custom-field magento-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 mins ago









Utsav Gupta

15012




15012










asked May 12 '17 at 11:53









RobRob

86111




86111













  • The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

    – Great Indian Brain
    Jul 25 '17 at 10:58



















  • The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

    – Great Indian Brain
    Jul 25 '17 at 10:58

















The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

– Great Indian Brain
Jul 25 '17 at 10:58





The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

– Great Indian Brain
Jul 25 '17 at 10:58










4 Answers
4






active

oldest

votes


















12














For adding image field, you can try using plugin and always try to avoid overwrite whole class.



Vendor/Module/etc/adminhtml/di.xml




<?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="MagentoUserBlockUserEditTabMain">
<plugin name="sr_stackexchange_user_form" type="VendorModulePluginBlockAdminhtmlUserEditTabMain" sortOrder="1"/>
</type>
</config>


Vendor/Module/Plugin/Block/Adminhtml/User/Edit/Tab/Main.php




namespace VendorModulePluginBlockAdminhtmlUserEditTab;

class Main
{
/**
* Get form HTML
*
* @return string
*/
public function aroundGetFormHtml(
MagentoUserBlockUserEditTabMain $subject,
Closure $proceed
)
{
$form = $subject->getForm();
if (is_object($form)) {
$fieldset = $form->addFieldset('admin_user_image', ['legend' => __('User Image')]);
$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();
}
}


Clear cache.






share|improve this answer
























  • Hey Sohel, thanks a lot for your response! It seems to be exact what I want to achieve :) I'll give you feedback as soon as I try this code locally. By the way, I saw that you create new fieldset and I'm starting to wonder if it's possible to update already existing one, e.g. 'base_fieldset', what do you think? Also, I am curious, is this plugin approach cover updating controllers too? I need to update some thinks in future here: /module-user/Controller/Adminhtml/User/Save.php - save string with image's path in 'admin_user' table. Sorry about many questions. appreciate your help! cheers!

    – Rob
    May 14 '17 at 18:29











  • Ok, it's possible to use plugin for a controller, but in my case that was not sufficient. Anyway, your suggestions help me to resolve a problem. Thank you once again!

    – Rob
    May 16 '17 at 5:58











  • The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

    – Great Indian Brain
    Jul 25 '17 at 10:59











  • I would also be interested in how to save the value of a new field in admin user form to admin_user table. Did you solve it extending/overriding the /module-user/Controller/Adminhtml/User/Save.php Controller?

    – hallleron
    Apr 24 '18 at 7:30











  • @Sohel Rana, selected field will not show where ? or how to we can get current user id here ?

    – SagarPPanchal
    Jun 29 '18 at 6:09



















1














after some research got a solution for this




add new property "value" in addField method




with the value you need.
see the example:



        $fieldset->addField(
'user_image',
'image',
[
'name' => 'user_image',
'label' => __('Image'),
'id' => 'user_image',
'title' => __('Image'),
'value' => $value_that_you_need,
'required' => false,
'note' => 'Allow image type: jpg, jpeg, png'
]
);


I hope it will help some of you ..






share|improve this answer

































    1














    Replacing the statement



    return parent::_prepareForm();


    with this



    return MagentoBackendBlockWidgetFormGeneric::_prepareForm();


    worked for me. Here is the complete code. Adding the field "Accessible Store" as follow.



    <?php
    /**
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */

    // @codingStandardsIgnoreFile

    namespace [vendor][module]BlockUserEditTab;

    use MagentoFrameworkAppObjectManager;
    use MagentoFrameworkLocaleOptionInterface;

    /**
    * Cms page edit form main tab
    *
    * @SuppressWarnings(PHPMD.DepthOfInheritance)
    */
    class Main extends MagentoUserBlockUserEditTabMain
    {

    /**
    * @param MagentoBackendBlockTemplateContext $context
    * @param MagentoFrameworkRegistry $registry
    * @param MagentoFrameworkDataFormFactory $formFactory
    * @param MagentoBackendModelAuthSession $authSession
    * @param MagentoFrameworkLocaleListsInterface $localeLists
    * @param array $data
    * @param OptionInterface $deployedLocales Operates with deployed locales.
    */

    public function __construct(
    MagentoBackendBlockTemplateContext $context,
    MagentoFrameworkRegistry $registry,
    MagentoFrameworkDataFormFactory $formFactory,
    MagentoBackendModelAuthSession $authSession,
    MagentoFrameworkLocaleListsInterface $localeLists,
    array $data = ,
    OptionInterface $deployedLocales = null
    ) {
    $this->deployedLocales = $deployedLocales
    ?: ObjectManager::getInstance()->get(OptionInterface::class);
    parent::__construct($context, $registry, $formFactory, $authSession, $localeLists, $data, $this->deployedLocales);
    }

    /**
    * Prepare form fields
    *
    * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
    * @return MagentoBackendBlockWidgetForm
    */
    protected function _prepareForm()
    {
    //die('test');
    /** @var $model MagentoUserModelUser */
    $model = $this->_coreRegistry->registry('permissions_user');

    /** @var MagentoFrameworkDataForm $form */
    $form = $this->_formFactory->create();
    $form->setHtmlIdPrefix('user_');

    $baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information')]);

    if ($model->getUserId()) {
    $baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
    } else {
    if (!$model->hasData('is_active')) {
    $model->setIsActive(1);
    }
    }

    $baseFieldset->addField(
    'username',
    'text',
    [
    'name' => 'username',
    'label' => __('User Name'),
    'id' => 'username',
    'title' => __('User Name'),
    'required' => true
    ]
    );

    $baseFieldset->addField(
    'firstname',
    'text',
    [
    'name' => 'firstname',
    'label' => __('First Name'),
    'id' => 'firstname',
    'title' => __('First Name'),
    'required' => true
    ]
    );

    $baseFieldset->addField(
    'lastname',
    'text',
    [
    'name' => 'lastname',
    'label' => __('Last Name'),
    'id' => 'lastname',
    'title' => __('Last Name'),
    'required' => true
    ]
    );

    // Adding new field for Scope Access
    $baseFieldset->addField(
    'accessible_store',
    'select',
    [
    'name' => 'accessible_store',
    'label' => __('Accessible Store'),
    'id' => 'accessible_store',
    'title' => __('Accessible Store'),
    'class' => 'input-select',
    'options' => ['3' => __('Global Store'),
    '1' => __('Malaysia Pavillion'),
    '2' => __('Thailand Pavilion')],
    'required' => true
    ]
    );

    $baseFieldset->addField(
    'email',
    'text',
    [
    'name' => 'email',
    'label' => __('Email'),
    'id' => 'customer_email',
    'title' => __('User Email'),
    'class' => 'required-entry validate-email',
    'required' => true
    ]
    );

    $isNewObject = $model->isObjectNew();
    if ($isNewObject) {
    $passwordLabel = __('Password');
    } else {
    $passwordLabel = __('New Password');
    }
    $confirmationLabel = __('Password Confirmation');
    $this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);

    $baseFieldset->addField(
    'interface_locale',
    'select',
    [
    'name' => 'interface_locale',
    'label' => __('Interface Locale'),
    'title' => __('Interface Locale'),
    'values' => $this->deployedLocales->getOptionLocales(),
    'class' => 'select'
    ]
    );

    if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
    $baseFieldset->addField(
    'is_active',
    'select',
    [
    'name' => 'is_active',
    'label' => __('This account is'),
    'id' => 'is_active',
    'title' => __('Account Status'),
    'class' => 'input-select',
    'options' => ['1' => __('Active'), '0' => __('Inactive')]
    ]
    );
    }

    $baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

    $currentUserVerificationFieldset = $form->addFieldset(
    'current_user_verification_fieldset',
    ['legend' => __('Current User Identity Verification')]
    );
    $currentUserVerificationFieldset->addField(
    self::CURRENT_USER_PASSWORD_FIELD,
    'password',
    [
    'name' => self::CURRENT_USER_PASSWORD_FIELD,
    'label' => __('Your Password'),
    'id' => self::CURRENT_USER_PASSWORD_FIELD,
    'title' => __('Your Password'),
    'class' => 'input-text validate-current-password required-entry',
    'required' => true
    ]
    );

    $data = $model->getData();
    unset($data['password']);
    unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
    $form->setValues($data);

    $this->setForm($form);

    //return parent::_prepareForm();
    return MagentoBackendBlockWidgetFormGeneric::_prepareForm();
    }

    }


    And thanks @Rob for sharing the clue where to start with.






    share|improve this answer































      0














      I just did a minor change in your solution and it worked for me :



      class Main extends MagentoBackendBlockWidgetFormGeneric
      {
      //Copied All the code in --- MagentoUserBlockUserEditTabMain
      //added my own field in _prepareForm function

      }


      If you want, I can post the whole solution -- but I have to revise it because as per my company norms I cannot show the code on public forums. So just let me know if you can do it yourself.






      share|improve this answer























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "479"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f174209%2fmagento-2-add-new-field-to-magento-user-admin-form%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        12














        For adding image field, you can try using plugin and always try to avoid overwrite whole class.



        Vendor/Module/etc/adminhtml/di.xml




        <?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="MagentoUserBlockUserEditTabMain">
        <plugin name="sr_stackexchange_user_form" type="VendorModulePluginBlockAdminhtmlUserEditTabMain" sortOrder="1"/>
        </type>
        </config>


        Vendor/Module/Plugin/Block/Adminhtml/User/Edit/Tab/Main.php




        namespace VendorModulePluginBlockAdminhtmlUserEditTab;

        class Main
        {
        /**
        * Get form HTML
        *
        * @return string
        */
        public function aroundGetFormHtml(
        MagentoUserBlockUserEditTabMain $subject,
        Closure $proceed
        )
        {
        $form = $subject->getForm();
        if (is_object($form)) {
        $fieldset = $form->addFieldset('admin_user_image', ['legend' => __('User Image')]);
        $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();
        }
        }


        Clear cache.






        share|improve this answer
























        • Hey Sohel, thanks a lot for your response! It seems to be exact what I want to achieve :) I'll give you feedback as soon as I try this code locally. By the way, I saw that you create new fieldset and I'm starting to wonder if it's possible to update already existing one, e.g. 'base_fieldset', what do you think? Also, I am curious, is this plugin approach cover updating controllers too? I need to update some thinks in future here: /module-user/Controller/Adminhtml/User/Save.php - save string with image's path in 'admin_user' table. Sorry about many questions. appreciate your help! cheers!

          – Rob
          May 14 '17 at 18:29











        • Ok, it's possible to use plugin for a controller, but in my case that was not sufficient. Anyway, your suggestions help me to resolve a problem. Thank you once again!

          – Rob
          May 16 '17 at 5:58











        • The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

          – Great Indian Brain
          Jul 25 '17 at 10:59











        • I would also be interested in how to save the value of a new field in admin user form to admin_user table. Did you solve it extending/overriding the /module-user/Controller/Adminhtml/User/Save.php Controller?

          – hallleron
          Apr 24 '18 at 7:30











        • @Sohel Rana, selected field will not show where ? or how to we can get current user id here ?

          – SagarPPanchal
          Jun 29 '18 at 6:09
















        12














        For adding image field, you can try using plugin and always try to avoid overwrite whole class.



        Vendor/Module/etc/adminhtml/di.xml




        <?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="MagentoUserBlockUserEditTabMain">
        <plugin name="sr_stackexchange_user_form" type="VendorModulePluginBlockAdminhtmlUserEditTabMain" sortOrder="1"/>
        </type>
        </config>


        Vendor/Module/Plugin/Block/Adminhtml/User/Edit/Tab/Main.php




        namespace VendorModulePluginBlockAdminhtmlUserEditTab;

        class Main
        {
        /**
        * Get form HTML
        *
        * @return string
        */
        public function aroundGetFormHtml(
        MagentoUserBlockUserEditTabMain $subject,
        Closure $proceed
        )
        {
        $form = $subject->getForm();
        if (is_object($form)) {
        $fieldset = $form->addFieldset('admin_user_image', ['legend' => __('User Image')]);
        $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();
        }
        }


        Clear cache.






        share|improve this answer
























        • Hey Sohel, thanks a lot for your response! It seems to be exact what I want to achieve :) I'll give you feedback as soon as I try this code locally. By the way, I saw that you create new fieldset and I'm starting to wonder if it's possible to update already existing one, e.g. 'base_fieldset', what do you think? Also, I am curious, is this plugin approach cover updating controllers too? I need to update some thinks in future here: /module-user/Controller/Adminhtml/User/Save.php - save string with image's path in 'admin_user' table. Sorry about many questions. appreciate your help! cheers!

          – Rob
          May 14 '17 at 18:29











        • Ok, it's possible to use plugin for a controller, but in my case that was not sufficient. Anyway, your suggestions help me to resolve a problem. Thank you once again!

          – Rob
          May 16 '17 at 5:58











        • The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

          – Great Indian Brain
          Jul 25 '17 at 10:59











        • I would also be interested in how to save the value of a new field in admin user form to admin_user table. Did you solve it extending/overriding the /module-user/Controller/Adminhtml/User/Save.php Controller?

          – hallleron
          Apr 24 '18 at 7:30











        • @Sohel Rana, selected field will not show where ? or how to we can get current user id here ?

          – SagarPPanchal
          Jun 29 '18 at 6:09














        12












        12








        12







        For adding image field, you can try using plugin and always try to avoid overwrite whole class.



        Vendor/Module/etc/adminhtml/di.xml




        <?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="MagentoUserBlockUserEditTabMain">
        <plugin name="sr_stackexchange_user_form" type="VendorModulePluginBlockAdminhtmlUserEditTabMain" sortOrder="1"/>
        </type>
        </config>


        Vendor/Module/Plugin/Block/Adminhtml/User/Edit/Tab/Main.php




        namespace VendorModulePluginBlockAdminhtmlUserEditTab;

        class Main
        {
        /**
        * Get form HTML
        *
        * @return string
        */
        public function aroundGetFormHtml(
        MagentoUserBlockUserEditTabMain $subject,
        Closure $proceed
        )
        {
        $form = $subject->getForm();
        if (is_object($form)) {
        $fieldset = $form->addFieldset('admin_user_image', ['legend' => __('User Image')]);
        $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();
        }
        }


        Clear cache.






        share|improve this answer













        For adding image field, you can try using plugin and always try to avoid overwrite whole class.



        Vendor/Module/etc/adminhtml/di.xml




        <?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="MagentoUserBlockUserEditTabMain">
        <plugin name="sr_stackexchange_user_form" type="VendorModulePluginBlockAdminhtmlUserEditTabMain" sortOrder="1"/>
        </type>
        </config>


        Vendor/Module/Plugin/Block/Adminhtml/User/Edit/Tab/Main.php




        namespace VendorModulePluginBlockAdminhtmlUserEditTab;

        class Main
        {
        /**
        * Get form HTML
        *
        * @return string
        */
        public function aroundGetFormHtml(
        MagentoUserBlockUserEditTabMain $subject,
        Closure $proceed
        )
        {
        $form = $subject->getForm();
        if (is_object($form)) {
        $fieldset = $form->addFieldset('admin_user_image', ['legend' => __('User Image')]);
        $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();
        }
        }


        Clear cache.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 12 '17 at 15:07









        Sohel RanaSohel Rana

        20.1k33853




        20.1k33853













        • Hey Sohel, thanks a lot for your response! It seems to be exact what I want to achieve :) I'll give you feedback as soon as I try this code locally. By the way, I saw that you create new fieldset and I'm starting to wonder if it's possible to update already existing one, e.g. 'base_fieldset', what do you think? Also, I am curious, is this plugin approach cover updating controllers too? I need to update some thinks in future here: /module-user/Controller/Adminhtml/User/Save.php - save string with image's path in 'admin_user' table. Sorry about many questions. appreciate your help! cheers!

          – Rob
          May 14 '17 at 18:29











        • Ok, it's possible to use plugin for a controller, but in my case that was not sufficient. Anyway, your suggestions help me to resolve a problem. Thank you once again!

          – Rob
          May 16 '17 at 5:58











        • The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

          – Great Indian Brain
          Jul 25 '17 at 10:59











        • I would also be interested in how to save the value of a new field in admin user form to admin_user table. Did you solve it extending/overriding the /module-user/Controller/Adminhtml/User/Save.php Controller?

          – hallleron
          Apr 24 '18 at 7:30











        • @Sohel Rana, selected field will not show where ? or how to we can get current user id here ?

          – SagarPPanchal
          Jun 29 '18 at 6:09



















        • Hey Sohel, thanks a lot for your response! It seems to be exact what I want to achieve :) I'll give you feedback as soon as I try this code locally. By the way, I saw that you create new fieldset and I'm starting to wonder if it's possible to update already existing one, e.g. 'base_fieldset', what do you think? Also, I am curious, is this plugin approach cover updating controllers too? I need to update some thinks in future here: /module-user/Controller/Adminhtml/User/Save.php - save string with image's path in 'admin_user' table. Sorry about many questions. appreciate your help! cheers!

          – Rob
          May 14 '17 at 18:29











        • Ok, it's possible to use plugin for a controller, but in my case that was not sufficient. Anyway, your suggestions help me to resolve a problem. Thank you once again!

          – Rob
          May 16 '17 at 5:58











        • The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

          – Great Indian Brain
          Jul 25 '17 at 10:59











        • I would also be interested in how to save the value of a new field in admin user form to admin_user table. Did you solve it extending/overriding the /module-user/Controller/Adminhtml/User/Save.php Controller?

          – hallleron
          Apr 24 '18 at 7:30











        • @Sohel Rana, selected field will not show where ? or how to we can get current user id here ?

          – SagarPPanchal
          Jun 29 '18 at 6:09

















        Hey Sohel, thanks a lot for your response! It seems to be exact what I want to achieve :) I'll give you feedback as soon as I try this code locally. By the way, I saw that you create new fieldset and I'm starting to wonder if it's possible to update already existing one, e.g. 'base_fieldset', what do you think? Also, I am curious, is this plugin approach cover updating controllers too? I need to update some thinks in future here: /module-user/Controller/Adminhtml/User/Save.php - save string with image's path in 'admin_user' table. Sorry about many questions. appreciate your help! cheers!

        – Rob
        May 14 '17 at 18:29





        Hey Sohel, thanks a lot for your response! It seems to be exact what I want to achieve :) I'll give you feedback as soon as I try this code locally. By the way, I saw that you create new fieldset and I'm starting to wonder if it's possible to update already existing one, e.g. 'base_fieldset', what do you think? Also, I am curious, is this plugin approach cover updating controllers too? I need to update some thinks in future here: /module-user/Controller/Adminhtml/User/Save.php - save string with image's path in 'admin_user' table. Sorry about many questions. appreciate your help! cheers!

        – Rob
        May 14 '17 at 18:29













        Ok, it's possible to use plugin for a controller, but in my case that was not sufficient. Anyway, your suggestions help me to resolve a problem. Thank you once again!

        – Rob
        May 16 '17 at 5:58





        Ok, it's possible to use plugin for a controller, but in my case that was not sufficient. Anyway, your suggestions help me to resolve a problem. Thank you once again!

        – Rob
        May 16 '17 at 5:58













        The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

        – Great Indian Brain
        Jul 25 '17 at 10:59





        The above solution is great but the values are not set on the added fields..is there anything else we need to do on the same. We are basically overriding the Reviews form. Thanks in advance..

        – Great Indian Brain
        Jul 25 '17 at 10:59













        I would also be interested in how to save the value of a new field in admin user form to admin_user table. Did you solve it extending/overriding the /module-user/Controller/Adminhtml/User/Save.php Controller?

        – hallleron
        Apr 24 '18 at 7:30





        I would also be interested in how to save the value of a new field in admin user form to admin_user table. Did you solve it extending/overriding the /module-user/Controller/Adminhtml/User/Save.php Controller?

        – hallleron
        Apr 24 '18 at 7:30













        @Sohel Rana, selected field will not show where ? or how to we can get current user id here ?

        – SagarPPanchal
        Jun 29 '18 at 6:09





        @Sohel Rana, selected field will not show where ? or how to we can get current user id here ?

        – SagarPPanchal
        Jun 29 '18 at 6:09













        1














        after some research got a solution for this




        add new property "value" in addField method




        with the value you need.
        see the example:



                $fieldset->addField(
        'user_image',
        'image',
        [
        'name' => 'user_image',
        'label' => __('Image'),
        'id' => 'user_image',
        'title' => __('Image'),
        'value' => $value_that_you_need,
        'required' => false,
        'note' => 'Allow image type: jpg, jpeg, png'
        ]
        );


        I hope it will help some of you ..






        share|improve this answer






























          1














          after some research got a solution for this




          add new property "value" in addField method




          with the value you need.
          see the example:



                  $fieldset->addField(
          'user_image',
          'image',
          [
          'name' => 'user_image',
          'label' => __('Image'),
          'id' => 'user_image',
          'title' => __('Image'),
          'value' => $value_that_you_need,
          'required' => false,
          'note' => 'Allow image type: jpg, jpeg, png'
          ]
          );


          I hope it will help some of you ..






          share|improve this answer




























            1












            1








            1







            after some research got a solution for this




            add new property "value" in addField method




            with the value you need.
            see the example:



                    $fieldset->addField(
            'user_image',
            'image',
            [
            'name' => 'user_image',
            'label' => __('Image'),
            'id' => 'user_image',
            'title' => __('Image'),
            'value' => $value_that_you_need,
            'required' => false,
            'note' => 'Allow image type: jpg, jpeg, png'
            ]
            );


            I hope it will help some of you ..






            share|improve this answer















            after some research got a solution for this




            add new property "value" in addField method




            with the value you need.
            see the example:



                    $fieldset->addField(
            'user_image',
            'image',
            [
            'name' => 'user_image',
            'label' => __('Image'),
            'id' => 'user_image',
            'title' => __('Image'),
            'value' => $value_that_you_need,
            'required' => false,
            'note' => 'Allow image type: jpg, jpeg, png'
            ]
            );


            I hope it will help some of you ..







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 12 '17 at 9:58









            Manoj Deswal

            4,27581742




            4,27581742










            answered Oct 12 '17 at 9:47









            tal shulgintal shulgin

            111




            111























                1














                Replacing the statement



                return parent::_prepareForm();


                with this



                return MagentoBackendBlockWidgetFormGeneric::_prepareForm();


                worked for me. Here is the complete code. Adding the field "Accessible Store" as follow.



                <?php
                /**
                * Copyright © Magento, Inc. All rights reserved.
                * See COPYING.txt for license details.
                */

                // @codingStandardsIgnoreFile

                namespace [vendor][module]BlockUserEditTab;

                use MagentoFrameworkAppObjectManager;
                use MagentoFrameworkLocaleOptionInterface;

                /**
                * Cms page edit form main tab
                *
                * @SuppressWarnings(PHPMD.DepthOfInheritance)
                */
                class Main extends MagentoUserBlockUserEditTabMain
                {

                /**
                * @param MagentoBackendBlockTemplateContext $context
                * @param MagentoFrameworkRegistry $registry
                * @param MagentoFrameworkDataFormFactory $formFactory
                * @param MagentoBackendModelAuthSession $authSession
                * @param MagentoFrameworkLocaleListsInterface $localeLists
                * @param array $data
                * @param OptionInterface $deployedLocales Operates with deployed locales.
                */

                public function __construct(
                MagentoBackendBlockTemplateContext $context,
                MagentoFrameworkRegistry $registry,
                MagentoFrameworkDataFormFactory $formFactory,
                MagentoBackendModelAuthSession $authSession,
                MagentoFrameworkLocaleListsInterface $localeLists,
                array $data = ,
                OptionInterface $deployedLocales = null
                ) {
                $this->deployedLocales = $deployedLocales
                ?: ObjectManager::getInstance()->get(OptionInterface::class);
                parent::__construct($context, $registry, $formFactory, $authSession, $localeLists, $data, $this->deployedLocales);
                }

                /**
                * Prepare form fields
                *
                * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
                * @return MagentoBackendBlockWidgetForm
                */
                protected function _prepareForm()
                {
                //die('test');
                /** @var $model MagentoUserModelUser */
                $model = $this->_coreRegistry->registry('permissions_user');

                /** @var MagentoFrameworkDataForm $form */
                $form = $this->_formFactory->create();
                $form->setHtmlIdPrefix('user_');

                $baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information')]);

                if ($model->getUserId()) {
                $baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
                } else {
                if (!$model->hasData('is_active')) {
                $model->setIsActive(1);
                }
                }

                $baseFieldset->addField(
                'username',
                'text',
                [
                'name' => 'username',
                'label' => __('User Name'),
                'id' => 'username',
                'title' => __('User Name'),
                'required' => true
                ]
                );

                $baseFieldset->addField(
                'firstname',
                'text',
                [
                'name' => 'firstname',
                'label' => __('First Name'),
                'id' => 'firstname',
                'title' => __('First Name'),
                'required' => true
                ]
                );

                $baseFieldset->addField(
                'lastname',
                'text',
                [
                'name' => 'lastname',
                'label' => __('Last Name'),
                'id' => 'lastname',
                'title' => __('Last Name'),
                'required' => true
                ]
                );

                // Adding new field for Scope Access
                $baseFieldset->addField(
                'accessible_store',
                'select',
                [
                'name' => 'accessible_store',
                'label' => __('Accessible Store'),
                'id' => 'accessible_store',
                'title' => __('Accessible Store'),
                'class' => 'input-select',
                'options' => ['3' => __('Global Store'),
                '1' => __('Malaysia Pavillion'),
                '2' => __('Thailand Pavilion')],
                'required' => true
                ]
                );

                $baseFieldset->addField(
                'email',
                'text',
                [
                'name' => 'email',
                'label' => __('Email'),
                'id' => 'customer_email',
                'title' => __('User Email'),
                'class' => 'required-entry validate-email',
                'required' => true
                ]
                );

                $isNewObject = $model->isObjectNew();
                if ($isNewObject) {
                $passwordLabel = __('Password');
                } else {
                $passwordLabel = __('New Password');
                }
                $confirmationLabel = __('Password Confirmation');
                $this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);

                $baseFieldset->addField(
                'interface_locale',
                'select',
                [
                'name' => 'interface_locale',
                'label' => __('Interface Locale'),
                'title' => __('Interface Locale'),
                'values' => $this->deployedLocales->getOptionLocales(),
                'class' => 'select'
                ]
                );

                if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
                $baseFieldset->addField(
                'is_active',
                'select',
                [
                'name' => 'is_active',
                'label' => __('This account is'),
                'id' => 'is_active',
                'title' => __('Account Status'),
                'class' => 'input-select',
                'options' => ['1' => __('Active'), '0' => __('Inactive')]
                ]
                );
                }

                $baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

                $currentUserVerificationFieldset = $form->addFieldset(
                'current_user_verification_fieldset',
                ['legend' => __('Current User Identity Verification')]
                );
                $currentUserVerificationFieldset->addField(
                self::CURRENT_USER_PASSWORD_FIELD,
                'password',
                [
                'name' => self::CURRENT_USER_PASSWORD_FIELD,
                'label' => __('Your Password'),
                'id' => self::CURRENT_USER_PASSWORD_FIELD,
                'title' => __('Your Password'),
                'class' => 'input-text validate-current-password required-entry',
                'required' => true
                ]
                );

                $data = $model->getData();
                unset($data['password']);
                unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
                $form->setValues($data);

                $this->setForm($form);

                //return parent::_prepareForm();
                return MagentoBackendBlockWidgetFormGeneric::_prepareForm();
                }

                }


                And thanks @Rob for sharing the clue where to start with.






                share|improve this answer




























                  1














                  Replacing the statement



                  return parent::_prepareForm();


                  with this



                  return MagentoBackendBlockWidgetFormGeneric::_prepareForm();


                  worked for me. Here is the complete code. Adding the field "Accessible Store" as follow.



                  <?php
                  /**
                  * Copyright © Magento, Inc. All rights reserved.
                  * See COPYING.txt for license details.
                  */

                  // @codingStandardsIgnoreFile

                  namespace [vendor][module]BlockUserEditTab;

                  use MagentoFrameworkAppObjectManager;
                  use MagentoFrameworkLocaleOptionInterface;

                  /**
                  * Cms page edit form main tab
                  *
                  * @SuppressWarnings(PHPMD.DepthOfInheritance)
                  */
                  class Main extends MagentoUserBlockUserEditTabMain
                  {

                  /**
                  * @param MagentoBackendBlockTemplateContext $context
                  * @param MagentoFrameworkRegistry $registry
                  * @param MagentoFrameworkDataFormFactory $formFactory
                  * @param MagentoBackendModelAuthSession $authSession
                  * @param MagentoFrameworkLocaleListsInterface $localeLists
                  * @param array $data
                  * @param OptionInterface $deployedLocales Operates with deployed locales.
                  */

                  public function __construct(
                  MagentoBackendBlockTemplateContext $context,
                  MagentoFrameworkRegistry $registry,
                  MagentoFrameworkDataFormFactory $formFactory,
                  MagentoBackendModelAuthSession $authSession,
                  MagentoFrameworkLocaleListsInterface $localeLists,
                  array $data = ,
                  OptionInterface $deployedLocales = null
                  ) {
                  $this->deployedLocales = $deployedLocales
                  ?: ObjectManager::getInstance()->get(OptionInterface::class);
                  parent::__construct($context, $registry, $formFactory, $authSession, $localeLists, $data, $this->deployedLocales);
                  }

                  /**
                  * Prepare form fields
                  *
                  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
                  * @return MagentoBackendBlockWidgetForm
                  */
                  protected function _prepareForm()
                  {
                  //die('test');
                  /** @var $model MagentoUserModelUser */
                  $model = $this->_coreRegistry->registry('permissions_user');

                  /** @var MagentoFrameworkDataForm $form */
                  $form = $this->_formFactory->create();
                  $form->setHtmlIdPrefix('user_');

                  $baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information')]);

                  if ($model->getUserId()) {
                  $baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
                  } else {
                  if (!$model->hasData('is_active')) {
                  $model->setIsActive(1);
                  }
                  }

                  $baseFieldset->addField(
                  'username',
                  'text',
                  [
                  'name' => 'username',
                  'label' => __('User Name'),
                  'id' => 'username',
                  'title' => __('User Name'),
                  'required' => true
                  ]
                  );

                  $baseFieldset->addField(
                  'firstname',
                  'text',
                  [
                  'name' => 'firstname',
                  'label' => __('First Name'),
                  'id' => 'firstname',
                  'title' => __('First Name'),
                  'required' => true
                  ]
                  );

                  $baseFieldset->addField(
                  'lastname',
                  'text',
                  [
                  'name' => 'lastname',
                  'label' => __('Last Name'),
                  'id' => 'lastname',
                  'title' => __('Last Name'),
                  'required' => true
                  ]
                  );

                  // Adding new field for Scope Access
                  $baseFieldset->addField(
                  'accessible_store',
                  'select',
                  [
                  'name' => 'accessible_store',
                  'label' => __('Accessible Store'),
                  'id' => 'accessible_store',
                  'title' => __('Accessible Store'),
                  'class' => 'input-select',
                  'options' => ['3' => __('Global Store'),
                  '1' => __('Malaysia Pavillion'),
                  '2' => __('Thailand Pavilion')],
                  'required' => true
                  ]
                  );

                  $baseFieldset->addField(
                  'email',
                  'text',
                  [
                  'name' => 'email',
                  'label' => __('Email'),
                  'id' => 'customer_email',
                  'title' => __('User Email'),
                  'class' => 'required-entry validate-email',
                  'required' => true
                  ]
                  );

                  $isNewObject = $model->isObjectNew();
                  if ($isNewObject) {
                  $passwordLabel = __('Password');
                  } else {
                  $passwordLabel = __('New Password');
                  }
                  $confirmationLabel = __('Password Confirmation');
                  $this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);

                  $baseFieldset->addField(
                  'interface_locale',
                  'select',
                  [
                  'name' => 'interface_locale',
                  'label' => __('Interface Locale'),
                  'title' => __('Interface Locale'),
                  'values' => $this->deployedLocales->getOptionLocales(),
                  'class' => 'select'
                  ]
                  );

                  if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
                  $baseFieldset->addField(
                  'is_active',
                  'select',
                  [
                  'name' => 'is_active',
                  'label' => __('This account is'),
                  'id' => 'is_active',
                  'title' => __('Account Status'),
                  'class' => 'input-select',
                  'options' => ['1' => __('Active'), '0' => __('Inactive')]
                  ]
                  );
                  }

                  $baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

                  $currentUserVerificationFieldset = $form->addFieldset(
                  'current_user_verification_fieldset',
                  ['legend' => __('Current User Identity Verification')]
                  );
                  $currentUserVerificationFieldset->addField(
                  self::CURRENT_USER_PASSWORD_FIELD,
                  'password',
                  [
                  'name' => self::CURRENT_USER_PASSWORD_FIELD,
                  'label' => __('Your Password'),
                  'id' => self::CURRENT_USER_PASSWORD_FIELD,
                  'title' => __('Your Password'),
                  'class' => 'input-text validate-current-password required-entry',
                  'required' => true
                  ]
                  );

                  $data = $model->getData();
                  unset($data['password']);
                  unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
                  $form->setValues($data);

                  $this->setForm($form);

                  //return parent::_prepareForm();
                  return MagentoBackendBlockWidgetFormGeneric::_prepareForm();
                  }

                  }


                  And thanks @Rob for sharing the clue where to start with.






                  share|improve this answer


























                    1












                    1








                    1







                    Replacing the statement



                    return parent::_prepareForm();


                    with this



                    return MagentoBackendBlockWidgetFormGeneric::_prepareForm();


                    worked for me. Here is the complete code. Adding the field "Accessible Store" as follow.



                    <?php
                    /**
                    * Copyright © Magento, Inc. All rights reserved.
                    * See COPYING.txt for license details.
                    */

                    // @codingStandardsIgnoreFile

                    namespace [vendor][module]BlockUserEditTab;

                    use MagentoFrameworkAppObjectManager;
                    use MagentoFrameworkLocaleOptionInterface;

                    /**
                    * Cms page edit form main tab
                    *
                    * @SuppressWarnings(PHPMD.DepthOfInheritance)
                    */
                    class Main extends MagentoUserBlockUserEditTabMain
                    {

                    /**
                    * @param MagentoBackendBlockTemplateContext $context
                    * @param MagentoFrameworkRegistry $registry
                    * @param MagentoFrameworkDataFormFactory $formFactory
                    * @param MagentoBackendModelAuthSession $authSession
                    * @param MagentoFrameworkLocaleListsInterface $localeLists
                    * @param array $data
                    * @param OptionInterface $deployedLocales Operates with deployed locales.
                    */

                    public function __construct(
                    MagentoBackendBlockTemplateContext $context,
                    MagentoFrameworkRegistry $registry,
                    MagentoFrameworkDataFormFactory $formFactory,
                    MagentoBackendModelAuthSession $authSession,
                    MagentoFrameworkLocaleListsInterface $localeLists,
                    array $data = ,
                    OptionInterface $deployedLocales = null
                    ) {
                    $this->deployedLocales = $deployedLocales
                    ?: ObjectManager::getInstance()->get(OptionInterface::class);
                    parent::__construct($context, $registry, $formFactory, $authSession, $localeLists, $data, $this->deployedLocales);
                    }

                    /**
                    * Prepare form fields
                    *
                    * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
                    * @return MagentoBackendBlockWidgetForm
                    */
                    protected function _prepareForm()
                    {
                    //die('test');
                    /** @var $model MagentoUserModelUser */
                    $model = $this->_coreRegistry->registry('permissions_user');

                    /** @var MagentoFrameworkDataForm $form */
                    $form = $this->_formFactory->create();
                    $form->setHtmlIdPrefix('user_');

                    $baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information')]);

                    if ($model->getUserId()) {
                    $baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
                    } else {
                    if (!$model->hasData('is_active')) {
                    $model->setIsActive(1);
                    }
                    }

                    $baseFieldset->addField(
                    'username',
                    'text',
                    [
                    'name' => 'username',
                    'label' => __('User Name'),
                    'id' => 'username',
                    'title' => __('User Name'),
                    'required' => true
                    ]
                    );

                    $baseFieldset->addField(
                    'firstname',
                    'text',
                    [
                    'name' => 'firstname',
                    'label' => __('First Name'),
                    'id' => 'firstname',
                    'title' => __('First Name'),
                    'required' => true
                    ]
                    );

                    $baseFieldset->addField(
                    'lastname',
                    'text',
                    [
                    'name' => 'lastname',
                    'label' => __('Last Name'),
                    'id' => 'lastname',
                    'title' => __('Last Name'),
                    'required' => true
                    ]
                    );

                    // Adding new field for Scope Access
                    $baseFieldset->addField(
                    'accessible_store',
                    'select',
                    [
                    'name' => 'accessible_store',
                    'label' => __('Accessible Store'),
                    'id' => 'accessible_store',
                    'title' => __('Accessible Store'),
                    'class' => 'input-select',
                    'options' => ['3' => __('Global Store'),
                    '1' => __('Malaysia Pavillion'),
                    '2' => __('Thailand Pavilion')],
                    'required' => true
                    ]
                    );

                    $baseFieldset->addField(
                    'email',
                    'text',
                    [
                    'name' => 'email',
                    'label' => __('Email'),
                    'id' => 'customer_email',
                    'title' => __('User Email'),
                    'class' => 'required-entry validate-email',
                    'required' => true
                    ]
                    );

                    $isNewObject = $model->isObjectNew();
                    if ($isNewObject) {
                    $passwordLabel = __('Password');
                    } else {
                    $passwordLabel = __('New Password');
                    }
                    $confirmationLabel = __('Password Confirmation');
                    $this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);

                    $baseFieldset->addField(
                    'interface_locale',
                    'select',
                    [
                    'name' => 'interface_locale',
                    'label' => __('Interface Locale'),
                    'title' => __('Interface Locale'),
                    'values' => $this->deployedLocales->getOptionLocales(),
                    'class' => 'select'
                    ]
                    );

                    if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
                    $baseFieldset->addField(
                    'is_active',
                    'select',
                    [
                    'name' => 'is_active',
                    'label' => __('This account is'),
                    'id' => 'is_active',
                    'title' => __('Account Status'),
                    'class' => 'input-select',
                    'options' => ['1' => __('Active'), '0' => __('Inactive')]
                    ]
                    );
                    }

                    $baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

                    $currentUserVerificationFieldset = $form->addFieldset(
                    'current_user_verification_fieldset',
                    ['legend' => __('Current User Identity Verification')]
                    );
                    $currentUserVerificationFieldset->addField(
                    self::CURRENT_USER_PASSWORD_FIELD,
                    'password',
                    [
                    'name' => self::CURRENT_USER_PASSWORD_FIELD,
                    'label' => __('Your Password'),
                    'id' => self::CURRENT_USER_PASSWORD_FIELD,
                    'title' => __('Your Password'),
                    'class' => 'input-text validate-current-password required-entry',
                    'required' => true
                    ]
                    );

                    $data = $model->getData();
                    unset($data['password']);
                    unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
                    $form->setValues($data);

                    $this->setForm($form);

                    //return parent::_prepareForm();
                    return MagentoBackendBlockWidgetFormGeneric::_prepareForm();
                    }

                    }


                    And thanks @Rob for sharing the clue where to start with.






                    share|improve this answer













                    Replacing the statement



                    return parent::_prepareForm();


                    with this



                    return MagentoBackendBlockWidgetFormGeneric::_prepareForm();


                    worked for me. Here is the complete code. Adding the field "Accessible Store" as follow.



                    <?php
                    /**
                    * Copyright © Magento, Inc. All rights reserved.
                    * See COPYING.txt for license details.
                    */

                    // @codingStandardsIgnoreFile

                    namespace [vendor][module]BlockUserEditTab;

                    use MagentoFrameworkAppObjectManager;
                    use MagentoFrameworkLocaleOptionInterface;

                    /**
                    * Cms page edit form main tab
                    *
                    * @SuppressWarnings(PHPMD.DepthOfInheritance)
                    */
                    class Main extends MagentoUserBlockUserEditTabMain
                    {

                    /**
                    * @param MagentoBackendBlockTemplateContext $context
                    * @param MagentoFrameworkRegistry $registry
                    * @param MagentoFrameworkDataFormFactory $formFactory
                    * @param MagentoBackendModelAuthSession $authSession
                    * @param MagentoFrameworkLocaleListsInterface $localeLists
                    * @param array $data
                    * @param OptionInterface $deployedLocales Operates with deployed locales.
                    */

                    public function __construct(
                    MagentoBackendBlockTemplateContext $context,
                    MagentoFrameworkRegistry $registry,
                    MagentoFrameworkDataFormFactory $formFactory,
                    MagentoBackendModelAuthSession $authSession,
                    MagentoFrameworkLocaleListsInterface $localeLists,
                    array $data = ,
                    OptionInterface $deployedLocales = null
                    ) {
                    $this->deployedLocales = $deployedLocales
                    ?: ObjectManager::getInstance()->get(OptionInterface::class);
                    parent::__construct($context, $registry, $formFactory, $authSession, $localeLists, $data, $this->deployedLocales);
                    }

                    /**
                    * Prepare form fields
                    *
                    * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
                    * @return MagentoBackendBlockWidgetForm
                    */
                    protected function _prepareForm()
                    {
                    //die('test');
                    /** @var $model MagentoUserModelUser */
                    $model = $this->_coreRegistry->registry('permissions_user');

                    /** @var MagentoFrameworkDataForm $form */
                    $form = $this->_formFactory->create();
                    $form->setHtmlIdPrefix('user_');

                    $baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information')]);

                    if ($model->getUserId()) {
                    $baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
                    } else {
                    if (!$model->hasData('is_active')) {
                    $model->setIsActive(1);
                    }
                    }

                    $baseFieldset->addField(
                    'username',
                    'text',
                    [
                    'name' => 'username',
                    'label' => __('User Name'),
                    'id' => 'username',
                    'title' => __('User Name'),
                    'required' => true
                    ]
                    );

                    $baseFieldset->addField(
                    'firstname',
                    'text',
                    [
                    'name' => 'firstname',
                    'label' => __('First Name'),
                    'id' => 'firstname',
                    'title' => __('First Name'),
                    'required' => true
                    ]
                    );

                    $baseFieldset->addField(
                    'lastname',
                    'text',
                    [
                    'name' => 'lastname',
                    'label' => __('Last Name'),
                    'id' => 'lastname',
                    'title' => __('Last Name'),
                    'required' => true
                    ]
                    );

                    // Adding new field for Scope Access
                    $baseFieldset->addField(
                    'accessible_store',
                    'select',
                    [
                    'name' => 'accessible_store',
                    'label' => __('Accessible Store'),
                    'id' => 'accessible_store',
                    'title' => __('Accessible Store'),
                    'class' => 'input-select',
                    'options' => ['3' => __('Global Store'),
                    '1' => __('Malaysia Pavillion'),
                    '2' => __('Thailand Pavilion')],
                    'required' => true
                    ]
                    );

                    $baseFieldset->addField(
                    'email',
                    'text',
                    [
                    'name' => 'email',
                    'label' => __('Email'),
                    'id' => 'customer_email',
                    'title' => __('User Email'),
                    'class' => 'required-entry validate-email',
                    'required' => true
                    ]
                    );

                    $isNewObject = $model->isObjectNew();
                    if ($isNewObject) {
                    $passwordLabel = __('Password');
                    } else {
                    $passwordLabel = __('New Password');
                    }
                    $confirmationLabel = __('Password Confirmation');
                    $this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);

                    $baseFieldset->addField(
                    'interface_locale',
                    'select',
                    [
                    'name' => 'interface_locale',
                    'label' => __('Interface Locale'),
                    'title' => __('Interface Locale'),
                    'values' => $this->deployedLocales->getOptionLocales(),
                    'class' => 'select'
                    ]
                    );

                    if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
                    $baseFieldset->addField(
                    'is_active',
                    'select',
                    [
                    'name' => 'is_active',
                    'label' => __('This account is'),
                    'id' => 'is_active',
                    'title' => __('Account Status'),
                    'class' => 'input-select',
                    'options' => ['1' => __('Active'), '0' => __('Inactive')]
                    ]
                    );
                    }

                    $baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

                    $currentUserVerificationFieldset = $form->addFieldset(
                    'current_user_verification_fieldset',
                    ['legend' => __('Current User Identity Verification')]
                    );
                    $currentUserVerificationFieldset->addField(
                    self::CURRENT_USER_PASSWORD_FIELD,
                    'password',
                    [
                    'name' => self::CURRENT_USER_PASSWORD_FIELD,
                    'label' => __('Your Password'),
                    'id' => self::CURRENT_USER_PASSWORD_FIELD,
                    'title' => __('Your Password'),
                    'class' => 'input-text validate-current-password required-entry',
                    'required' => true
                    ]
                    );

                    $data = $model->getData();
                    unset($data['password']);
                    unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
                    $form->setValues($data);

                    $this->setForm($form);

                    //return parent::_prepareForm();
                    return MagentoBackendBlockWidgetFormGeneric::_prepareForm();
                    }

                    }


                    And thanks @Rob for sharing the clue where to start with.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 8 at 6:47









                    saiidsaiid

                    6422822




                    6422822























                        0














                        I just did a minor change in your solution and it worked for me :



                        class Main extends MagentoBackendBlockWidgetFormGeneric
                        {
                        //Copied All the code in --- MagentoUserBlockUserEditTabMain
                        //added my own field in _prepareForm function

                        }


                        If you want, I can post the whole solution -- but I have to revise it because as per my company norms I cannot show the code on public forums. So just let me know if you can do it yourself.






                        share|improve this answer




























                          0














                          I just did a minor change in your solution and it worked for me :



                          class Main extends MagentoBackendBlockWidgetFormGeneric
                          {
                          //Copied All the code in --- MagentoUserBlockUserEditTabMain
                          //added my own field in _prepareForm function

                          }


                          If you want, I can post the whole solution -- but I have to revise it because as per my company norms I cannot show the code on public forums. So just let me know if you can do it yourself.






                          share|improve this answer


























                            0












                            0








                            0







                            I just did a minor change in your solution and it worked for me :



                            class Main extends MagentoBackendBlockWidgetFormGeneric
                            {
                            //Copied All the code in --- MagentoUserBlockUserEditTabMain
                            //added my own field in _prepareForm function

                            }


                            If you want, I can post the whole solution -- but I have to revise it because as per my company norms I cannot show the code on public forums. So just let me know if you can do it yourself.






                            share|improve this answer













                            I just did a minor change in your solution and it worked for me :



                            class Main extends MagentoBackendBlockWidgetFormGeneric
                            {
                            //Copied All the code in --- MagentoUserBlockUserEditTabMain
                            //added my own field in _prepareForm function

                            }


                            If you want, I can post the whole solution -- but I have to revise it because as per my company norms I cannot show the code on public forums. So just let me know if you can do it yourself.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 17 '18 at 12:49









                            Abid MalikAbid Malik

                            4810




                            4810






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Magento Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f174209%2fmagento-2-add-new-field-to-magento-user-admin-form%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Polycentropodidae

                                Magento 2 Error message: Invalid state change requested

                                Paulmy