Pergunta

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="Magento\User\Block\User\Edit\Tab\Main" type="Vendor_Name\Module_Name\Block\User\Edit\Tab\Main" />
<preference for="Magento\User\Block\Role\Grid\User" type="Vendor_Name\Module_Name\Block\Role\Grid\User" />

`

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

// @codingStandardsIgnoreFile

namespace Vendor_Name\Module_Name\Block\User\Edit\Tab;

use \Magento\User\Block\User\Edit\Tab\Main as UserEditMainTab;
use \Magento\Backend\Block\Template\Context;
use \Magento\Framework\Registry;
use \Magento\Framework\Data\FormFactory;
use \Magento\Backend\Model\Auth\Session;
use \Magento\Framework\Locale\ListsInterface;

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 \Magento\User\Model\User */
        $model = $this->_coreRegistry->registry('permissions_user');

        /** @var \Magento\Framework\Data\Form $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_Name\Module_Name\Block\Role\Grid;

use \Magento\User\Block\Role\Grid\User as RoleGridUser;
use \Magento\Backend\Block\Widget\Grid\Extended 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?

Foi útil?

Solução

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="Magento\User\Block\User\Edit\Tab\Main">
        <plugin name="sr_stackexchange_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


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('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.

Outras dicas

Replacing the statement

return parent::_prepareForm();

with this

return \Magento\Backend\Block\Widget\Form\Generic::_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]\Block\User\Edit\Tab;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Locale\OptionInterface;

/**
 * Cms page edit form main tab
 *
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 */
class Main extends \Magento\User\Block\User\Edit\Tab\Main
{

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Backend\Model\Auth\Session $authSession
     * @param \Magento\Framework\Locale\ListsInterface $localeLists
     * @param array $data
     * @param OptionInterface $deployedLocales Operates with deployed locales.
     */

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Backend\Model\Auth\Session $authSession,
        \Magento\Framework\Locale\ListsInterface $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 \Magento\Backend\Block\Widget\Form
     */
    protected function _prepareForm()
    {
        //die('test');
        /** @var $model \Magento\User\Model\User */
        $model = $this->_coreRegistry->registry('permissions_user');

        /** @var \Magento\Framework\Data\Form $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 \Magento\Backend\Block\Widget\Form\Generic::_prepareForm();
    }

}

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

Just to add another working example, I've succeeded in overwriting the Website admin page. I was trying to add a URL field to the website edit page.

I did exactly what it's said in the validated answer but I didn't add a new fieldset. Instead, I've completed the existing one, using it's id defined in the Website class.

Besides, I'v used the inheritance to retrieve the website model and retrieve the current value from database to put it inside the form (It's copied from the Magento Website class too).

In prerequisite, it's needed to add the column url in the store_website table of the Magento database.

Here is the working result (tested in Magento 2.1) :

<?php

namespace Vendor\Store\Plugin\Block\System\Store\Edit\Form;

class Website extends \Magento\Backend\Block\System\Store\Edit\Form\Website
{
    /**
     * Get form HTML
     *
     * @return string
     */
    public function aroundGetFormHtml(
        \Magento\Backend\Block\System\Store\Edit\Form\Website $subject,
        \Closure $proceed
    )
    {
        $form = $subject->getForm();
        if (is_object($form)) {

            // From \Magento\Backend\Block\System\Store\Edit\Form\Website :
            $websiteModel = $this->_coreRegistry->registry('store_data');
            $postData = $this->_coreRegistry->registry('store_post_data');
            if ($postData) {
                $websiteModel->setData($postData['website']);
            }

            // Fieldset name from \Magento\Backend\Block\System\Store\Edit\Form\Website
            $fieldset = $form->getElement('website_fieldset');
            $fieldset->addField(
                'website_url',
                'text',
                [
                    'name' => 'website[url]', // From \Magento\Backend\Block\System\Store\Edit\Form\Website
                    'label' => __('Website URL'),
                    'value' => $websiteModel->getData('url'),
                    'title' => __('Website URL'),
                    'required' => false
                ]
            );

            $subject->setForm($form);
        }

        return $proceed();
    }
}

And the di.xml file in Vendor/Store/etc/adminhtml directory (nothing new here from the validated answer) :

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Backend\Block\System\Store\Edit\Form\Website">
        <plugin name="admin_website_plugin" type="Vendor\Store\Plugin\Block\System\Store\Edit\Form\Website" sortOrder="1"/>
    </type>
</config>

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 ..

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

class Main extends \Magento\Backend\Block\Widget\Form\Generic
{
//Copied All the code in --- Magento\User\Block\User\Edit\Tab\Main
//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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top