0

How can I add a new field which has an integer value into admin user info?

I want to add a dropdown on admin user edit form, to select any one admin user which has 'ABC' role, and that will be saved with that admin user, for that, I found a way that we can add a new column into the "admin/user" table and save that selected value when admin user edited, not on admin user create.

How can achieve this functionality with the custom module?

I used below code and its saved manager id in table but in edit form it will not showing selected value. code as below :

protected $manageData;

public function __construct(
    \Name\Module\Model\Entity\Attribute\Source\ManageData $manageData
) {
    $this->manageData = $manageData;
}

/**
 * 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)) {
        $baseFieldset = $form->addFieldset('admin_manageData', ['legend' => __('Manager Information')]);
        $baseFieldset->addField(
            'manageData',
            'select',
            [
                'name' => 'manageData',
                'label' => __('Select Manager'),
                'title' => __('Select Manager'),
                'values' => $this->manageData->getAllOptions(), // it has array of all the admin users with role 'Manager' 
                'class' => 'select'
            ]
        );

        $subject->setForm($form);
    }

    return $proceed();
}

Thanks.

2

1 Answer 1

3

Try following way:


namespace SR\MagentoStackExchange\Plugin\Block\Adminhtml\User\Edit\Tab;

class Main
{
    private $registry;

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

    /**
     * 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)) {
            $baseFieldset = $form->getElement('base_fieldset');
            /** @var $model \Magento\User\Model\User */
            $model = $this->registry->registry('permissions_user');
            $data = $model->getData();
            $baseFieldset->addField(
                'manage_data',
                'select',
                [
                    'name' => 'manage_data',
                    'label' => __('Select Manager'),
                    'title' => __('Select Manager'),
                    'options' => ['0' => __('--Select--'), '1' => __('Option 1'), '2' => __('Option 2'), '3' => __('Option 3')],
                    'class' => 'select',
                    'value' => isset($data['manage_data']) ? $data['manage_data'] : 0
                ]
            );

            $subject->setForm($form);
        }

        return $proceed();
    }
}

Now new field will adding under Account Information fieldsets. Also value will be selected. I use manage_data column name.

Your Answer

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

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