I have created a module in magento2 to upload image field. Saving the image hiding the image upload field option in backend
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
if($data) {
if(isset($data['is_active']) && $data['is_active'] === 'true') {
$data['is_active'] = Notification::STATUS_ENABLED;
}
if(empty($data['notification_id'])){
$data['notification_id'] = null;
}
if(isset($data['image']) && is_array($data['image'])){
$data['image']=$data['image'][0]['name'];
}
else{
$data['image'] = null;
}
/** @var \Dzinehub\PushNotification\Model\Notification $notification */
$model = $this->notificationFactory->create();
$id = $this->getRequest()->getParam('notification_id');
if($id) {
try{
$model = $this->notificationRepository->getById($id);
} catch(LocalizedException $e) {
$this->messageManager->addErrorMessage(__('Notification no longer exists'));
return $resultRedirect->setPath('*/*/');
}
}
$model->setData($data);
/* var_dump($data);
die;*/
try {
$this->notificationRepository->save($model);
$this->messageManager->addSuccessMessage(__('You saved the notification.'));
$this->dataPersistor->clear('dzpushnotif');
if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath('*/*/edit', ['notification_id' => $model->getId()]);
}
return $resultRedirect->setPath('*/*/');
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the notification.'));
}
$this->dataPersistor->set('dzpushnotif', $data);
return $resultRedirect->setPath('*/*/edit', ['notification_id' => $this->getRequest()->getParam('notification_id')]);
}
return $resultRedirect->setPath('*/*/');
}
Also, how to move the image from temporary directory using this function moveFileFromTmp. Any help on this issue?