2

I am trying to add an image to each admin user.

I have added a form field with a plugin:

namespace Vendor\UserImage\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' => 'Allowed image types: jpg, jpeg, gif, png'
              ]
          );

          $subject->setForm($form);
      }

      return $proceed();
  }
}

Now, I have added a controller which should save the file to a given folder, but the controller is not being called at all. Here is the controller code:

namespace Vendor\UserImage\Controller\Adminhtml\User;

use Magento\Framework\App\Filesystem\DirectoryList;

class Save extends \Magento\User\Controller\Adminhtml\User
{
    protected $_requestData;

    /**
     * Upload max file size in kilobytes
     *
     * @var int
     */
    protected $_maxFileSize = 0;

    /**
     * @var Filesystem
     */
    protected $_filesystem;

    /**
     * @var \Magento\Framework\Filesystem\Directory\WriteInterface
     */
    protected $_mediaDirectory;

    /**
     * @var \Magento\MediaStorage\Model\File\UploaderFactory
     */
    protected $_uploaderFactory;

    /**
     * @param \Magento\Framework\Model\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
     * @param \Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface $requestData
     * @param Filesystem $filesystem
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
     * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
     * @param array $data
     */

     public function __construct(
         \Magento\Framework\Model\Context $context,
         \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
         Filesystem $filesystem
     ) {
         $this->_uploaderFactory = $uploaderFactory;
         $this->_filesystem = $filesystem;
         $this->_mediaDirectory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
         parent::__construct();
     }

     /**
      * Save uploaded file before saving config value
      *
      * @return $this
      * @throws \Magento\Framework\Exception\LocalizedException
      */
     public function execute()
     {
         die('test123');
         $fileName = 'user_image';
         $profileImage = (int)$this->getRequest()->getParam($fileName);
         if (!empty($profileImage)) {
             $uploadDir = $this->_mediaDirectory->getAbsolutePath('userimages/');
             try {
                 /** @var Uploader $uploader */
                 $uploader = $this->_uploaderFactory->create(['fileId' => $fileName]);
                 $uploader->setAllowCreateFolders(true);
                 $uploader->setAllowRenameFiles(true);
                 $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
                 $uploader->addValidateCallback('size', $this, 'validateMaxSize');
                 $result = $uploader->save($uploadDir);
             } catch (\Exception $e) {
                 throw new \Magento\Framework\Exception\LocalizedException(__('%1', $e->getMessage()));
             }

         return $this;
        }

}

vendor/userimage/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="user_image_form" type="Vendor\UserImage\Plugin\Block\Adminhtml\User\Edit\Tab\Main" sortOrder="1"/>
    </type>
</config>

The user is still being saved, which should not happen because of the die-function in the controller.

Anyone who can point me in the right direction?

4
  • Where is your di.xml ?
    – Keyur Shah
    Commented Oct 3, 2017 at 8:43
  • vendor/userimage/etc/adminhtml/di.xml
    – perrivdb
    Commented Oct 3, 2017 at 8:52
  • Post your di.xml
    – Keyur Shah
    Commented Oct 3, 2017 at 8:53
  • updated the original question with my di.xml
    – perrivdb
    Commented Oct 3, 2017 at 8:55

1 Answer 1

3

From the di.xml It looks like you have written the code for overwrite controller but you forgot to define in di.xml , So first thing you need to overwrite the controller with the help of di.xml like this,

<preference for="Magento\User\Controller\Adminhtml\User\Save" type="Vendor\UserImage\Controller\Adminhtml\User\Save" />

Also, in your controller Vendor\UserImage\Controller\Adminhtml\User\Save you should extend the Magento\User\Controller\Adminhtml\User\Save class. You can also read tutorial about overwrite controller from this link or this

3
  • Thanks for your reply! However, I am not trying to override the existing controller, I just want to add the uploader functionality to it. Will this work when I override the original controller in di.xml, and then extend the original one in the new controller?
    – perrivdb
    Commented Oct 3, 2017 at 9:17
  • Yes after overriding It will work , you can also use plugin feature If you do not want to override the controller
    – Keyur Shah
    Commented Oct 3, 2017 at 9:33
  • it is giving Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Developer\Customizer\Controller\Adminhtml\User\Save\Interceptor Commented Sep 1, 2020 at 6:05

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.