0

I want to add an extra attribute to the admin user form. I searched but unable to find anything guide me plz.enter image description here

3 Answers 3

3

The answer of Ghulam.M. works perfectly, but it doesn't show the correct value once you saved, due to the empty value on addValues. I improved the answer with showing the correct values.

namespace Vendor\Module\Plugin\Block\Adminhtml\User\Edit\Tab;
class Main
{
    /** @var \Magento\Framework\Registry  */
    protected $_coreRegistry;

    public function __construct(\Magento\Framework\Registry $registry)
    {
        $this->_coreRegistry = $registry;
    }

    /**
     * Get form HTML
     *
     * @return string
     */
    public function aroundGetFormHtml(
        \Magento\User\Block\User\Edit\Tab\Main $subject,
        \Closure $proceed
    )
    {
        /** @var $model \Magento\User\Model\User */
        $model = $this->_coreRegistry->registry('permissions_user');

        $form = $subject->getForm();
        if (is_object($form)) {
            $fieldset = $form->addFieldset('cus_fieldsetname_code', ['legend' => __('Authorization Code')]);
            $fieldset->addField(
                'custom_column',
                'text',
                [
                    'name' => 'custom_column',
                    'label' => __('Custom Column'),
                    'id' => 'custom_column',
                    'title' => __('custom_column'),
                    'required' => false,
                    'note' => 'Custom column '
                ]
            );
            $form->addValues(
                [
                    'custom_column' => $model->getData('custom_column'),
                ]
            );
            $subject->setForm($form);
        }
        return $proceed();
    }
} 
2

I have done somewhat similar to this for this first you have to upgrade the admin_user table using UpgradeSchema

UpgradeSchema.php

<?php
    namespace Vendor\Module\Setup;
     use Magento\Framework\Setup\UpgradeSchemaInterface;
    use Magento\Framework\Setup\SchemaSetupInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
     class UpgradeSchema implements UpgradeSchemaInterface
    {
        public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $setup->startSetup();
             //handle all possible upgrade versions
             $installer = $setup;
            $installer->startSetup();
            $connection = $installer->getConnection();
             if (version_compare($context->getVersion(), '1.0.1') < 0) {
                //code to upgrade to 1.0.1
                if ($connection->tableColumnExists('admin_user', 'custom_column') === false) {
                    $connection
                        ->addColumn(
                            $setup->getTable('admin_user'),
                            'custom_column',
                            [
                                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                'length' => 256,
                                'nullable' => true,
                                'comment' => 'custom_column'
                            ]
                        );
                }
             }
             $setup->endSetup();
        }
    } 

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="Magento\User\Block\User\Edit\Tab\Main">
        <plugin name="admin_user_field" type="Vendor\Module\Plugin\Block\Adminhtml\User\Edit\Tab\Main" sortOrder="1"/>
    </type>
</config> 

Main.php

    <?php
namespace Vendor\Module\Plugin\Block\Adminhtml\User\Edit\Tab;
 class Main
{
    /**
     * Get form HTML
     *
     * @return string
     */
    public function aroundGetFormHtml(
        \Magento\User\Block\User\Edit\Tab\Main $subject,
        \Closure $proceed
    )
    {
        $form = $subject->getForm();
        if (is_object($form)) {
            $fieldset = $form->addFieldset('cus_fieldsetname_code', ['legend' => __('Authorization Code')]);
            $fieldset->addField(
                'custom_column',
                'text',
                [
                    'name' => 'custom_column',
                    'label' => __('Custom Column'),
                    'id' => 'custom_column',
                    'title' => __('custom_column'),
                    'required' => false,
                    'note' => 'Custom column '
                ]
            );
             $form->addValues(
                [
                    'custom_column' => '', 
                ]
            );
             $subject->setForm($form);
         }
         return $proceed();
    }
} 
2

To make the answer of Ghulam.M work with M2.3 declartive schema you can replace the UpgradeSchema file with the new declarative schema file which would look as follows:

<?xml version="1.0"?>

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="admin_user">
        <column xsi:type="smallint" name="custom_column" padding="5" unsigned="true" nullable="false" identity="false"
        default="0" comment="custom_column"/>
    </table>
</schema>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.