On this weblog, we’re going to discover ways to create a multilanguage file add subject within the HelperForm Class of PrestaShop.
So let’s see collectively how to do this with PrestaShop.
Typically, we have to add information language-wise within the module.
We are going to add the file add subject to the module configuration web page utilizing the renderForm operate.
public operate renderForm()
{
$fields_form = [
'form' => [
'legend' => [
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
],
'enter' => [
[
'type' => 'file_lang',
'label' => $this->l('Multilang image'),
'name' => 'WK_MULTILANG_IMG',
'lang' => true,
]
],
'submit' => [
'title' => $this->l('Save'),
],
],
];
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->desk = $this->desk;
$helper->default_form_language = $lang->id;
$helper->module = $this;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitStoreConf';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->title . '&tab_module=' . $this->tab . '&module_name=' . $this->title;
$helper->token = Instruments::getAdminTokenLite('AdminModules');
$helper->tpl_vars = [
'uri' => $this->getPathUri(),
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
];
return $helper->generateForm([$fields_form]);
}Within the above code, we used the file kind subject and ‘kind’ => ‘file_lang’ as an alternative of ‘kind’ => ‘file’ and added new params ‘lang’ => true for multilanguage.
The remaining code is identical as we used for type creation.
Now we’ll create type.tpl file on the under path
module_name/views/templates/admin/_configure/helpers/type/
We’ve got overridden the shape.tpl file utilizing our module and use the under code within the type.tpl file
{extends file="helpers/type/type.tpl"}
{block title="subject"}
{if $enter.kind == 'file_lang'}
<div class="col-lg-8">
{foreach from=$languages merchandise=language}
depend > 1
<div class="translatable-field lang-{$language.id_lang}" {if $language.id_lang != $defaultFormLanguage}type="show:none"{/if}>
{/if}
<div class="form-group">
<div class="col-lg-6">
<enter id="{$enter.title}_{$language.id_lang}" kind="file" title="{$enter.title}_{$language.id_lang}" class="cover" />
<div class="dummyfile input-group">
<span class="input-group-addon"><i class="icon-file"></i></span>
<enter id="{$enter.title}_{$language.id_lang}-name" kind="textual content" class="disabled" title="filename" readonly />
<span class="input-group-btn">
<button id="{$enter.title}_{$language.id_lang}-selectbutton" kind="button" title="submitAddAttachments" class="btn btn-default">
<i class="icon-folder-open"></i> {l s='Select a file' mod='wkmultilangfile'}
</button>
</span>
</div>
</div>
depend > 1
<div class="col-lg-2">
<button kind="button" class="btn btn-default dropdown-toggle" tabindex="-1" data-toggle="dropdown">
{$language.iso_code}
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
{foreach from=$languages merchandise=lang}
<li><a href="https://webkul.com/weblog/create-a-multilanguage-file-upload-field-in-the-prestashop/javascript:hideOtherLanguage({$lang.id_lang});" id="dropdown-lang-item-link-{$lang.id_lang}" tabindex="-1">{$lang.title}</a></li>
{/foreach}
</ul>
</div>
{/if}
</div>
<div class="form-group">
{if isset($fields_value[$input.name][$language.id_lang]) && $fields_value[$input.name][$language.id_lang] != ''}
<div id="{$enter.title}-{$language.id_lang}-images-thumbnails" class="col-lg-12">
<img src="{$uri}img/{$fields_value[$input.name][$language.id_lang]}" class="img-thumbnail"/>
</div>
{/if}
</div>
depend > 1
</div>
{/if}
<script>
$(doc).prepared(operate(){
$('#{$enter.title}_{$language.id_lang}-selectbutton').click on(operate(e){
$('#{$enter.title}_{$language.id_lang}').set off('click on');
});
$('#{$enter.title}_{$language.id_lang}').change(operate(e){
var val = $(this).val();
var file = val.cut up(/[/]/);
$('#{$enter.title}_{$language.id_lang}-name').val(file[file.length-1]);
});
});
</script>
{/foreach}
{if isset($enter.desc) && !empty($enter.desc)}
<p class="help-block">
{$enter.desc}
</p>
{/if}
</div>
{else}
{$smarty.block.dad or mum}
{/if}
{/block}Within the above code, we prolonged the helper type tpl and overridden the sector block.
we’ve checked the if situation for the file_lang kind subject and wrote the multilanguage subject code and within the else half return the dad or mum subject block for different fields.
Now file add subject will seem language-wise.

Now we’ll save the photographs language-wise utilizing the code under.
public operate postProcess()
{
if (Instruments::isSubmit('submitStoreConf')) {
$languages = Language::getLanguages(false);
$values = [];
$update_images_values = false;
foreach ($languages as $lang) {
if (isset($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']])
&& isset($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['tmp_name'])
&& !empty($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['tmp_name'])) {
if ($error = ImageManager::validateUpload($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']], 4000000)) {
return $this->displayError($error);
} else {
$ext = substr($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['name'], strrpos($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['name'], '.') + 1);
$file_name = md5($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['name']) . '.' . $ext;
if (!move_uploaded_file($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['tmp_name'], dirname(__FILE__) . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . $file_name)) {
return $this->displayError($this->l('An error occurred whereas making an attempt to add the file.'));
} else {
if (Configuration::hasContext('WK_MULTILANG_IMG', $lang['id_lang'], Store::getContext())
&& Configuration::get('WK_MULTILANG_IMG', $lang['id_lang']) != $file_name) {
@unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . Configuration::get('WK_MULTILANG_IMG', $lang['id_lang']));
}
$values['WK_MULTILANG_IMG'][$lang['id_lang']] = $file_name;
}
}
$update_images_values = true;
}
}
if ($update_images_values && isset($values['WK_MULTILANG_IMG'])) {
Configuration::updateValue('WK_MULTILANG_IMG', $values['WK_MULTILANG_IMG']);
}
return $this->displayConfirmation($this->l('The settings have been up to date.'));
}
return '';
}Right here we did it on the configuration web page.
We are able to do the identical step for the admin controller type.
That’s all about this weblog. Hope it would enable you to.
In case you are going through any points or doubts within the above course of, please be at liberty to contact us within the remark part.
We’d be glad to assist.
Additionally, you’ll be able to discover our PrestaShop Improvement Companies & a wide range of high quality PrestaShop Modules.
For any doubt contact us at [email protected].
On this weblog, we’re going to discover ways to create a multilanguage file add subject within the HelperForm Class of PrestaShop.
So let’s see collectively how to do this with PrestaShop.
Typically, we have to add information language-wise within the module.
We are going to add the file add subject to the module configuration web page utilizing the renderForm operate.
public operate renderForm()
{
$fields_form = [
'form' => [
'legend' => [
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
],
'enter' => [
[
'type' => 'file_lang',
'label' => $this->l('Multilang image'),
'name' => 'WK_MULTILANG_IMG',
'lang' => true,
]
],
'submit' => [
'title' => $this->l('Save'),
],
],
];
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->desk = $this->desk;
$helper->default_form_language = $lang->id;
$helper->module = $this;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitStoreConf';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->title . '&tab_module=' . $this->tab . '&module_name=' . $this->title;
$helper->token = Instruments::getAdminTokenLite('AdminModules');
$helper->tpl_vars = [
'uri' => $this->getPathUri(),
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
];
return $helper->generateForm([$fields_form]);
}Within the above code, we used the file kind subject and ‘kind’ => ‘file_lang’ as an alternative of ‘kind’ => ‘file’ and added new params ‘lang’ => true for multilanguage.
The remaining code is identical as we used for type creation.
Now we’ll create type.tpl file on the under path
module_name/views/templates/admin/_configure/helpers/type/
We’ve got overridden the shape.tpl file utilizing our module and use the under code within the type.tpl file
{extends file="helpers/type/type.tpl"}
{block title="subject"}
{if $enter.kind == 'file_lang'}
<div class="col-lg-8">
{foreach from=$languages merchandise=language}
depend > 1
<div class="translatable-field lang-{$language.id_lang}" {if $language.id_lang != $defaultFormLanguage}type="show:none"{/if}>
{/if}
<div class="form-group">
<div class="col-lg-6">
<enter id="{$enter.title}_{$language.id_lang}" kind="file" title="{$enter.title}_{$language.id_lang}" class="cover" />
<div class="dummyfile input-group">
<span class="input-group-addon"><i class="icon-file"></i></span>
<enter id="{$enter.title}_{$language.id_lang}-name" kind="textual content" class="disabled" title="filename" readonly />
<span class="input-group-btn">
<button id="{$enter.title}_{$language.id_lang}-selectbutton" kind="button" title="submitAddAttachments" class="btn btn-default">
<i class="icon-folder-open"></i> {l s='Select a file' mod='wkmultilangfile'}
</button>
</span>
</div>
</div>
depend > 1
<div class="col-lg-2">
<button kind="button" class="btn btn-default dropdown-toggle" tabindex="-1" data-toggle="dropdown">
{$language.iso_code}
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
{foreach from=$languages merchandise=lang}
<li><a href="https://webkul.com/weblog/create-a-multilanguage-file-upload-field-in-the-prestashop/javascript:hideOtherLanguage({$lang.id_lang});" id="dropdown-lang-item-link-{$lang.id_lang}" tabindex="-1">{$lang.title}</a></li>
{/foreach}
</ul>
</div>
{/if}
</div>
<div class="form-group">
{if isset($fields_value[$input.name][$language.id_lang]) && $fields_value[$input.name][$language.id_lang] != ''}
<div id="{$enter.title}-{$language.id_lang}-images-thumbnails" class="col-lg-12">
<img src="{$uri}img/{$fields_value[$input.name][$language.id_lang]}" class="img-thumbnail"/>
</div>
{/if}
</div>
depend > 1
</div>
{/if}
<script>
$(doc).prepared(operate(){
$('#{$enter.title}_{$language.id_lang}-selectbutton').click on(operate(e){
$('#{$enter.title}_{$language.id_lang}').set off('click on');
});
$('#{$enter.title}_{$language.id_lang}').change(operate(e){
var val = $(this).val();
var file = val.cut up(/[/]/);
$('#{$enter.title}_{$language.id_lang}-name').val(file[file.length-1]);
});
});
</script>
{/foreach}
{if isset($enter.desc) && !empty($enter.desc)}
<p class="help-block">
{$enter.desc}
</p>
{/if}
</div>
{else}
{$smarty.block.dad or mum}
{/if}
{/block}Within the above code, we prolonged the helper type tpl and overridden the sector block.
we’ve checked the if situation for the file_lang kind subject and wrote the multilanguage subject code and within the else half return the dad or mum subject block for different fields.
Now file add subject will seem language-wise.

Now we’ll save the photographs language-wise utilizing the code under.
public operate postProcess()
{
if (Instruments::isSubmit('submitStoreConf')) {
$languages = Language::getLanguages(false);
$values = [];
$update_images_values = false;
foreach ($languages as $lang) {
if (isset($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']])
&& isset($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['tmp_name'])
&& !empty($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['tmp_name'])) {
if ($error = ImageManager::validateUpload($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']], 4000000)) {
return $this->displayError($error);
} else {
$ext = substr($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['name'], strrpos($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['name'], '.') + 1);
$file_name = md5($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['name']) . '.' . $ext;
if (!move_uploaded_file($_FILES['WK_MULTILANG_IMG_' . $lang['id_lang']]['tmp_name'], dirname(__FILE__) . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . $file_name)) {
return $this->displayError($this->l('An error occurred whereas making an attempt to add the file.'));
} else {
if (Configuration::hasContext('WK_MULTILANG_IMG', $lang['id_lang'], Store::getContext())
&& Configuration::get('WK_MULTILANG_IMG', $lang['id_lang']) != $file_name) {
@unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . Configuration::get('WK_MULTILANG_IMG', $lang['id_lang']));
}
$values['WK_MULTILANG_IMG'][$lang['id_lang']] = $file_name;
}
}
$update_images_values = true;
}
}
if ($update_images_values && isset($values['WK_MULTILANG_IMG'])) {
Configuration::updateValue('WK_MULTILANG_IMG', $values['WK_MULTILANG_IMG']);
}
return $this->displayConfirmation($this->l('The settings have been up to date.'));
}
return '';
}Right here we did it on the configuration web page.
We are able to do the identical step for the admin controller type.
That’s all about this weblog. Hope it would enable you to.
In case you are going through any points or doubts within the above course of, please be at liberty to contact us within the remark part.
We’d be glad to assist.
Additionally, you’ll be able to discover our PrestaShop Improvement Companies & a wide range of high quality PrestaShop Modules.
For any doubt contact us at [email protected].



