2

By default when we create new admin user, it has the fields for Username, First Name, Last Name, Email id, Password, etc.

I want to create new field to save mobile number of all admin users and want to list all admin's with contact numbers while creating order shipment, but I'm not getting how to do it.

Please help me to sort out this issue.

Thanks in advance.

0

To add new field in admin user form, first of all you need to create a new column in admin_user table.

app/code/local/{Namespace}/{Module}/sql/{module_setup}/mysql4-install-{version}.php

<?php
/* @var $installer Mage_Customer_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();

$user = $this->getTable('admin/user');
$installer->run("
    ALTER TABLE  $user ADD  `mobile` varchar(255) NOT NULL
");

$installer->endSetup();

Then you need to observer adminhtml_block_html_before event to add new field in admin user form.

app/code/community/{Namespace}/{Module}/etc/config.xml

<?xml version="1.0"?>
<config>
    ...
    <adminhtml>
        ...
        <events>
            <adminhtml_block_html_before>
                <observers>
                    <event_column_append>
                        <type>model</type>
                        <class>{your-observer-class}</class>
                        <method>addMobileField</method>
                    </event_column_append>
                </observers>
            </adminhtml_block_html_before>
        </events>
        ...
    </adminhtml>
    ...
</config>

app/code/community/{Namespace}/{Module}/Model/Observer.php

<?php
class {Namespace}_{Module}_Model_Observer
{
   public function addMobileField(Varien_Event_Observer $observer) {
        $block = $observer->getEvent()->getBlock();
        if (!isset($block)) {
            return $this;
        }
        if ($block->getType() == 'adminhtml/system_account_edit_form') {
            $form = $block->getForm();

            $fieldset = $form->getElement('base_fieldset');

            //add new website field
            $fieldset->addField('mobile', 'text', array(
                    'name'  => 'mobile',
                    'label' => Mage::helper('adminhtml')->__('Mobile No.'),
                    'title' => Mage::helper('adminhtml')->__('Mobile No.'),
                    'required' => false,
                ),
                'email'
            );

            $userId = Mage::getSingleton('admin/session')->getUser()->getId();
            $user = Mage::getModel('admin/user')
                ->load($userId);
            $user->unsetData('password');
            $form->setValues($user->getData());
        }
    }
}

And at last, you need to override Mage_Adminhtml_System_AccountController controller file to save mobile field.

...
/**
 * Saving edited user information
 */
public function saveAction()
{
    ...
    $user->setId($userId)
        ->setUsername($this->getRequest()->getParam('username', false))
        ->setFirstname($this->getRequest()->getParam('firstname', false))
        ->setLastname($this->getRequest()->getParam('lastname', false))
        ->setEmail(strtolower($this->getRequest()->getParam('email', false)))
        ->setMobile($this->getRequest()->getParam('mobile', false));
    ...
}
...
  • Thank you very much for your reply. This solution helped me, Field for mobile number is visible in 'My Account' form of admin. But saveAction() of controller is not executed. How to do it? – Ajinkya Nov 28 '17 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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