0

I am looking to add an image upload when creating/editing admin users.

I am trying to figure out the best way to do this. I have already added a custom text field to this form using a plugin.

Magento\User\Block\User\Edit\Tab\Main

Here is the code for the modlue

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">
        <column xsi:type="varchar" name="supplier_name" nullable="true" comment="Supplier Name"/>
        <column xsi:type="varchar" name="supplier_logo" nullable="true" comment="Supplier Logo"/>
    </table>
</schema>

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="Mage\ExtendUsers\Plugin\Block\Adminhtml\User\Edit\Tab\Main" sortOrder="1"/>
        </type>
</config>

Main.php

<?php

namespace Mage\ExtendUsers\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('amdin_supplier_name', ['legend' => __('Supplier')]);
            $fieldset->addField(
                'supplier_name',
                'text',
                [
                    'name' => 'supplier_name',
                    'label' => __('Supplier Name'),
                    'id' => 'supplier_name',
                    'title' => __('supplier_name'),
                    'required' => false
                ]
            );

            $fieldset->addField(
                'supplier_logo',
                'image',
                [
                    'name'  => 'supplier_logo',
                    'label' => __('Supplier Logo'),
                    'title' => __('Supplier Logo'),
                    'required'  => false,
                ]
            );

            $form->addValues(
                [
                    'supplier_name' => $model->getData('supplier_name'),
                    'supplier_logo' => $model->getData('supplier_logo'),
                ]
            );
            $subject->setForm($form);
        }
        return $proceed();
    }
}

When I save the form it save the data for both fields in the database.

I was looking into creating an event on admin_user_save_before to grab the upload information.

Is this the best way to go about it or is there a better way.

Another issue i am facing is that once and image has been added and I try and edit the form and resave I get this error message.

Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3105

Which then disappears if I remove the image upload field.

Your Answer

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

Browse other questions tagged or ask your own question.