0

I have created a custom field in admin user creation form ,how to save that field in admin_user table in Magento2?

4
  • add your custom column to admin_user table to store your new field. and field name and column name must be same.
    – user83874
    Commented Feb 22, 2020 at 4:14
  • @MrMakwana,Thanks for your reply ..but I have created the custom field like the way you explained above. On saving the admin user creation form , the value in custom field are not saved to the table admin_user Commented Feb 22, 2020 at 5:28
  • please share your save code to more clarification
    – user83874
    Commented Feb 22, 2020 at 6:40
  • I didn't write it .I'm asking about how to do that Commented Feb 22, 2020 at 7:09

1 Answer 1

1

I have added a new field(telephone) in the database table admin_user using the following.

Vendor/Module/etc/db_schema.xml

<?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" resource="default" engine="innodb" comment="Admin User Table">
        <column xsi:type="varchar" name="telephone" nullable="true" length="255" comment="Admin User Telephone"/>
    </table>
</schema>

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

Vendor/Module/Plugin/Block/Adminhtml/User/Edit/Tab/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->getElement('base_fieldset');
                /** @var $model \Magento\User\Model\User */
                $model = $this->registry->registry('permissions_user');
                $data = $model->getData();
                $fieldset->addField(
                    'telephone',
                    'select',
                    [
                        'name' => 'telephone',
                        'label' => __('Select Telephone'),
                        'title' => __('Select Telephone'),
                        'options' => ['0' => __('--Select--'), '1' => __('Option 1'), '2' => __('Option 2'), '3' => __('Option 3')],
                        'class' => 'select',
                        'value' => isset($data['telephone']) ? $data['telephone'] : 0
                    ]
                );

                $subject->setForm($form);
            }

            return $proceed();
        }
    }

Hope this will work for you.

4
  • ,Thanks..Now Value is updated in the admin_user table, but the issue is the value is not reflected inthe admin user creation form. I have created the select option field ,while saving the correct value is updated in the admin_user table but ,while viewing that user form the field displays default value Commented Feb 22, 2020 at 9:03
  • I have updated my answer, Please check Commented Feb 22, 2020 at 9:26
  • Is worked for you? Commented Feb 22, 2020 at 10:22
  • Thank you so much @kishor .It was perfectly worked for me.It saves my time...Thanks again Commented Feb 22, 2020 at 10:26

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.