Magento 2 Admin Config - Add dynamic Image field programmatically - Setoptions
Good afternoon,
I'm a little new to this Magento 2 game so any advice will be happily received.
I am trying to add a dynamic image field to my admin configuration tab, so far I have the field appearing but I can't save and or preview an uploaded image.
I can get this working fine with the static option from the system.xml using
<field id="imageupload" type="image" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>upload</label>
<backend_model>MagentoConfigModelConfigBackendImage</backend_model>
<upload_dir config="system/filesystem/media" scope_info="1">catalog/product/placeholder</upload_dir>
<base_url type="media" scope_info="1">catalog/product/placeholder</base_url>
</field>
However, I'm missing something when trying to replicate this in PHP, here is what I have so far
[
'id' => 'allergen' . $option->getValue(),
'type' => 'image',
'sortOrder' => ($index * 10),
'showInDefault' => '1',
'showInWebsite' => '0',
'showInStore' => '0',
'label' => 'Image for ' . $option->getLabel(),
'backend_model' => 'MagentoConfigModelConfigBackendImage',
'base_url' => ['value' => 'catalog/product/images', 'type'=>"media", 'scope_info'=>"1"],
'upload_dir' => ['value'=> 'catalog/product/images', 'config'=> "system/filesystem/media", 'scope_info'=>"1"],
'_elementType' => 'field',
'path' => '<sectionId>/<groupid>'
]
The image appears to be saving via the default backend model as text and not respecting the input image value
magento2 admin configuration image
add a comment |
Good afternoon,
I'm a little new to this Magento 2 game so any advice will be happily received.
I am trying to add a dynamic image field to my admin configuration tab, so far I have the field appearing but I can't save and or preview an uploaded image.
I can get this working fine with the static option from the system.xml using
<field id="imageupload" type="image" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>upload</label>
<backend_model>MagentoConfigModelConfigBackendImage</backend_model>
<upload_dir config="system/filesystem/media" scope_info="1">catalog/product/placeholder</upload_dir>
<base_url type="media" scope_info="1">catalog/product/placeholder</base_url>
</field>
However, I'm missing something when trying to replicate this in PHP, here is what I have so far
[
'id' => 'allergen' . $option->getValue(),
'type' => 'image',
'sortOrder' => ($index * 10),
'showInDefault' => '1',
'showInWebsite' => '0',
'showInStore' => '0',
'label' => 'Image for ' . $option->getLabel(),
'backend_model' => 'MagentoConfigModelConfigBackendImage',
'base_url' => ['value' => 'catalog/product/images', 'type'=>"media", 'scope_info'=>"1"],
'upload_dir' => ['value'=> 'catalog/product/images', 'config'=> "system/filesystem/media", 'scope_info'=>"1"],
'_elementType' => 'field',
'path' => '<sectionId>/<groupid>'
]
The image appears to be saving via the default backend model as text and not respecting the input image value
magento2 admin configuration image
add a comment |
Good afternoon,
I'm a little new to this Magento 2 game so any advice will be happily received.
I am trying to add a dynamic image field to my admin configuration tab, so far I have the field appearing but I can't save and or preview an uploaded image.
I can get this working fine with the static option from the system.xml using
<field id="imageupload" type="image" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>upload</label>
<backend_model>MagentoConfigModelConfigBackendImage</backend_model>
<upload_dir config="system/filesystem/media" scope_info="1">catalog/product/placeholder</upload_dir>
<base_url type="media" scope_info="1">catalog/product/placeholder</base_url>
</field>
However, I'm missing something when trying to replicate this in PHP, here is what I have so far
[
'id' => 'allergen' . $option->getValue(),
'type' => 'image',
'sortOrder' => ($index * 10),
'showInDefault' => '1',
'showInWebsite' => '0',
'showInStore' => '0',
'label' => 'Image for ' . $option->getLabel(),
'backend_model' => 'MagentoConfigModelConfigBackendImage',
'base_url' => ['value' => 'catalog/product/images', 'type'=>"media", 'scope_info'=>"1"],
'upload_dir' => ['value'=> 'catalog/product/images', 'config'=> "system/filesystem/media", 'scope_info'=>"1"],
'_elementType' => 'field',
'path' => '<sectionId>/<groupid>'
]
The image appears to be saving via the default backend model as text and not respecting the input image value
magento2 admin configuration image
Good afternoon,
I'm a little new to this Magento 2 game so any advice will be happily received.
I am trying to add a dynamic image field to my admin configuration tab, so far I have the field appearing but I can't save and or preview an uploaded image.
I can get this working fine with the static option from the system.xml using
<field id="imageupload" type="image" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>upload</label>
<backend_model>MagentoConfigModelConfigBackendImage</backend_model>
<upload_dir config="system/filesystem/media" scope_info="1">catalog/product/placeholder</upload_dir>
<base_url type="media" scope_info="1">catalog/product/placeholder</base_url>
</field>
However, I'm missing something when trying to replicate this in PHP, here is what I have so far
[
'id' => 'allergen' . $option->getValue(),
'type' => 'image',
'sortOrder' => ($index * 10),
'showInDefault' => '1',
'showInWebsite' => '0',
'showInStore' => '0',
'label' => 'Image for ' . $option->getLabel(),
'backend_model' => 'MagentoConfigModelConfigBackendImage',
'base_url' => ['value' => 'catalog/product/images', 'type'=>"media", 'scope_info'=>"1"],
'upload_dir' => ['value'=> 'catalog/product/images', 'config'=> "system/filesystem/media", 'scope_info'=>"1"],
'_elementType' => 'field',
'path' => '<sectionId>/<groupid>'
]
The image appears to be saving via the default backend model as text and not respecting the input image value
magento2 admin configuration image
magento2 admin configuration image
edited Jul 28 '18 at 8:29
Dan S
asked Jul 27 '18 at 15:05
Dan SDan S
13
13
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
<field id="slider_image_1" translate="label" type="image" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Slider Image 1</label>
<comment>Allowed file types: jpg, jpeg, gif, png</comment>
<!-- backend model which save uploaded file on define location -->
<backend_model>CodilarProductViewModelSaveimage</backend_model>
<base_url type="media" scope_info="1">codilar/banner</base_url>
</field>
In saveImage write this
class Saveimage extends MagentoConfigModelConfigBackendImage
{
/**
* The tail part of directory path for uploading
*/
const UPLOAD_DIR = 'codilar/banner';
/**
* Upload max file size in kilobytes
*
* @var int
*/
protected $_maxFileSize = 2048;
/**
* Return path to directory for upload file
*
* @return string
*/
protected function _getUploadDir()
{
return $this->_mediaDirectory->getAbsolutePath($this->_appendScopeInfo(self::UPLOAD_DIR));
}
/**
* Makes a decision about whether to add info about the scope
*
* @return boolean
*/
protected function _addWhetherScopeInfo()
{
return true;
}
/**
* @return MagentoConfigModelConfigBackendImage
* @throws MagentoFrameworkExceptionFileSystemException
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function beforeSave()
{
$value = $this->getValue();
$deleteFlag = is_array($value) && !empty($value['delete']);
$fileTmpName = $_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value'];
if ($this->getOldValue() && ($fileTmpName || $deleteFlag)) {
$this->_mediaDirectory->delete(self::UPLOAD_DIR . '/' . $this->getOldValue());
}
return parent::beforeSave();
}
}
add a comment |
im doing the same thing but i cant upload the image from the image field, because its not taking the backend_model. Plz help if anybody know what is the issue.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f236290%2fmagento-2-admin-config-add-dynamic-image-field-programmatically-setoptions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
<field id="slider_image_1" translate="label" type="image" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Slider Image 1</label>
<comment>Allowed file types: jpg, jpeg, gif, png</comment>
<!-- backend model which save uploaded file on define location -->
<backend_model>CodilarProductViewModelSaveimage</backend_model>
<base_url type="media" scope_info="1">codilar/banner</base_url>
</field>
In saveImage write this
class Saveimage extends MagentoConfigModelConfigBackendImage
{
/**
* The tail part of directory path for uploading
*/
const UPLOAD_DIR = 'codilar/banner';
/**
* Upload max file size in kilobytes
*
* @var int
*/
protected $_maxFileSize = 2048;
/**
* Return path to directory for upload file
*
* @return string
*/
protected function _getUploadDir()
{
return $this->_mediaDirectory->getAbsolutePath($this->_appendScopeInfo(self::UPLOAD_DIR));
}
/**
* Makes a decision about whether to add info about the scope
*
* @return boolean
*/
protected function _addWhetherScopeInfo()
{
return true;
}
/**
* @return MagentoConfigModelConfigBackendImage
* @throws MagentoFrameworkExceptionFileSystemException
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function beforeSave()
{
$value = $this->getValue();
$deleteFlag = is_array($value) && !empty($value['delete']);
$fileTmpName = $_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value'];
if ($this->getOldValue() && ($fileTmpName || $deleteFlag)) {
$this->_mediaDirectory->delete(self::UPLOAD_DIR . '/' . $this->getOldValue());
}
return parent::beforeSave();
}
}
add a comment |
<field id="slider_image_1" translate="label" type="image" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Slider Image 1</label>
<comment>Allowed file types: jpg, jpeg, gif, png</comment>
<!-- backend model which save uploaded file on define location -->
<backend_model>CodilarProductViewModelSaveimage</backend_model>
<base_url type="media" scope_info="1">codilar/banner</base_url>
</field>
In saveImage write this
class Saveimage extends MagentoConfigModelConfigBackendImage
{
/**
* The tail part of directory path for uploading
*/
const UPLOAD_DIR = 'codilar/banner';
/**
* Upload max file size in kilobytes
*
* @var int
*/
protected $_maxFileSize = 2048;
/**
* Return path to directory for upload file
*
* @return string
*/
protected function _getUploadDir()
{
return $this->_mediaDirectory->getAbsolutePath($this->_appendScopeInfo(self::UPLOAD_DIR));
}
/**
* Makes a decision about whether to add info about the scope
*
* @return boolean
*/
protected function _addWhetherScopeInfo()
{
return true;
}
/**
* @return MagentoConfigModelConfigBackendImage
* @throws MagentoFrameworkExceptionFileSystemException
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function beforeSave()
{
$value = $this->getValue();
$deleteFlag = is_array($value) && !empty($value['delete']);
$fileTmpName = $_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value'];
if ($this->getOldValue() && ($fileTmpName || $deleteFlag)) {
$this->_mediaDirectory->delete(self::UPLOAD_DIR . '/' . $this->getOldValue());
}
return parent::beforeSave();
}
}
add a comment |
<field id="slider_image_1" translate="label" type="image" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Slider Image 1</label>
<comment>Allowed file types: jpg, jpeg, gif, png</comment>
<!-- backend model which save uploaded file on define location -->
<backend_model>CodilarProductViewModelSaveimage</backend_model>
<base_url type="media" scope_info="1">codilar/banner</base_url>
</field>
In saveImage write this
class Saveimage extends MagentoConfigModelConfigBackendImage
{
/**
* The tail part of directory path for uploading
*/
const UPLOAD_DIR = 'codilar/banner';
/**
* Upload max file size in kilobytes
*
* @var int
*/
protected $_maxFileSize = 2048;
/**
* Return path to directory for upload file
*
* @return string
*/
protected function _getUploadDir()
{
return $this->_mediaDirectory->getAbsolutePath($this->_appendScopeInfo(self::UPLOAD_DIR));
}
/**
* Makes a decision about whether to add info about the scope
*
* @return boolean
*/
protected function _addWhetherScopeInfo()
{
return true;
}
/**
* @return MagentoConfigModelConfigBackendImage
* @throws MagentoFrameworkExceptionFileSystemException
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function beforeSave()
{
$value = $this->getValue();
$deleteFlag = is_array($value) && !empty($value['delete']);
$fileTmpName = $_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value'];
if ($this->getOldValue() && ($fileTmpName || $deleteFlag)) {
$this->_mediaDirectory->delete(self::UPLOAD_DIR . '/' . $this->getOldValue());
}
return parent::beforeSave();
}
}
<field id="slider_image_1" translate="label" type="image" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Slider Image 1</label>
<comment>Allowed file types: jpg, jpeg, gif, png</comment>
<!-- backend model which save uploaded file on define location -->
<backend_model>CodilarProductViewModelSaveimage</backend_model>
<base_url type="media" scope_info="1">codilar/banner</base_url>
</field>
In saveImage write this
class Saveimage extends MagentoConfigModelConfigBackendImage
{
/**
* The tail part of directory path for uploading
*/
const UPLOAD_DIR = 'codilar/banner';
/**
* Upload max file size in kilobytes
*
* @var int
*/
protected $_maxFileSize = 2048;
/**
* Return path to directory for upload file
*
* @return string
*/
protected function _getUploadDir()
{
return $this->_mediaDirectory->getAbsolutePath($this->_appendScopeInfo(self::UPLOAD_DIR));
}
/**
* Makes a decision about whether to add info about the scope
*
* @return boolean
*/
protected function _addWhetherScopeInfo()
{
return true;
}
/**
* @return MagentoConfigModelConfigBackendImage
* @throws MagentoFrameworkExceptionFileSystemException
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function beforeSave()
{
$value = $this->getValue();
$deleteFlag = is_array($value) && !empty($value['delete']);
$fileTmpName = $_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value'];
if ($this->getOldValue() && ($fileTmpName || $deleteFlag)) {
$this->_mediaDirectory->delete(self::UPLOAD_DIR . '/' . $this->getOldValue());
}
return parent::beforeSave();
}
}
answered Sep 14 '18 at 13:22
Mguru48Mguru48
411
411
add a comment |
add a comment |
im doing the same thing but i cant upload the image from the image field, because its not taking the backend_model. Plz help if anybody know what is the issue.
add a comment |
im doing the same thing but i cant upload the image from the image field, because its not taking the backend_model. Plz help if anybody know what is the issue.
add a comment |
im doing the same thing but i cant upload the image from the image field, because its not taking the backend_model. Plz help if anybody know what is the issue.
im doing the same thing but i cant upload the image from the image field, because its not taking the backend_model. Plz help if anybody know what is the issue.
answered 18 mins ago
Faran CheemaFaran Cheema
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f236290%2fmagento-2-admin-config-add-dynamic-image-field-programmatically-setoptions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown