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?
di.xml