If you want to upload an image then why don't you use image selection button.?
If you like the editor then use it.But it is not a proper way to upload an image using an editor.You can use the button instead. If you don't know how to do it. Let me explain.
Here is my code.
The below code is written in block file which creates a button.
$fieldset->addField(
'image',
'file',
[
'name' => 'image',
'label' => __('Image'),
'title' => __('Image'),
]
);
Image is the database field name. In your case it's wysiwyg editor.I don't know the exact but once check in your database.
The below code is used to save the image in your table. Now put this code into your Controller.
<?php
namespace Vendor\Module\Controller\Adminhtml\Slider;
use Magento\Framework\App\Filesystem\DirectoryList;
class Save extends \Magento\Backend\App\Action
{
protected $_mediaDirectory;
protected $_fileUploaderFactory;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Filesystem $filesystem,
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
)
{
$this->_mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$this->_fileUploaderFactory = $fileUploaderFactory;
parent::__construct($context);
}
public function execute()
{
/*For Image Upload*/
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
try{
$target = $this->_mediaDirectory->getAbsolutePath('imagefolder/');
$targetOne = "imagefolder/";
/** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
$uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);
/** Allowed extension types */
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png', 'zip', 'doc']);
/** rename file name if already exists */
$uploader->setAllowRenameFiles(true);
/** upload file in folder "mycustomfolder" */
$result = $uploader->save($target);
/*If file found then display message*/
if ($result['file'])
{
$this->messageManager->addSuccess(__('File has been successfully uploaded'));
}
}
catch (Exception $e)
{
$this->messageManager->addError($e->getMessage());
}
/*For Image Upload Finished*/
$data = $this->getRequest()->getPostValue();
$data['image'] = $targetOne.$result['file'];
if (!$data) {
$this->_redirect('*/*/filenaem');
return;
}
try {
$rowData = $this->_objectManager->create('Vendor\Module\Model\Slider');
$rowData->setData($data);
if (isset($data['id']))
{
$rowData->setEntityId($data['id']);
}
$rowData->save();
$this->messageManager->addSuccess(__('Row data has been successfully saved.'));
}
catch (Exception $e)
{
$this->messageManager->addError(__($e->getMessage()));
}
$this->_redirect('*/*/index');
return $this->resultRedirectFactory->create()->setPath(
'*/*/upload', ['_secure'=>$this->getRequest()->isSecure()]
);
}
/**
* Check Category Map permission.
*
* @return bool
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Vendor_Module::Module_list');
}
}
After that you want to called it in phtml for the result..so bellow code write in phtml file.
Here is code.
$collection = $block->getCollectionFor();
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of\Magento\Framework\App\ObjectManager
$storeManager = $_objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStore = $storeManager->getStore();
//Base URL for saving image into database.
$mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
getCollectionFor() is write in my block.so according to that, you should apply as your block file.
I hope this is helpful to you. If you have any query let me know.
You have probably solved your issue but for people who come here from google as me, there is a solution:
When creating the customer attribute, please take care of the following things:
Attribute is added to the attribute set, group
customer_eav_attribute
eav_entity_attribute
Attribute is assigned to customer forms
customer_form_attribute
And the last one the most important as somehow, people just skip over it and wondering why the customer attribute does not want to save from the backend: Please make sure to set "is_system" flag in "customer_eav_attribute" table to 0, otherwise the attribute will not be saved.
It can be done by setting the attribute option to "system" => 0 in attribute parameters inside the install/upgrade script.
Don't forget to flush your cache after all!
Solution URL:
direct link to solution
Best Answer
Use
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
, In your constructor argument and set the class property:$this->scopeConfig = $scopeConfig;
Now to Get the configuration value just use