how to add an admin tab without extending a controller in prestashop? - prestashop

i would like to add admin tab, i defined a controller and install the tab with this function:
public function installTab($class_name, $tabname) {
$tab = new Tab();
// Define the title of your tab that will be displayed in BO
$tab->name[$this->context->language->id] = $tabname;
// Name of your admin controller
$tab->class_name = $class_name;
// Id of the controller where the tab will be attached
// If you want to attach it to the root, it will be id 0 (I'll explain it below)
$tab->id_parent = 0;
$tab->active = 1;
// Name of your module, if you're not working in a module, just ignore it, it will be set to null in DB
$tab->module = $this->name;
// Other field like the position will be set to the last, if you want to put it to the top you'll have to modify the position fields directly in your DB
$tab->add();
return true;
}
and my controller is defined like this:
class AdminBarCodeGeneratorAdminController extends AdminController
{
/** #var Smarty */
public $smarty;
public function __construct(){
parent::__construct();
}
public function initContent()
{
parent::initContent();
$scan_form=$this->renderForm();
$this->smarty->assign('scan_form',$scan_form);
$this->setTemplate(_PS_MODULE_DIR_.'BarCodeGenerator/views/templates/admin/tabs/scan.tpl');
}
// public function display(){
// $smarty = $this->context->smarty;
// $scan_form=$this->renderForm();
// $smarty->assign('scan_form',$scan_form);
// return $this->display(__FILE__, 'views/templates/admin/tabs/scan.tpl');
// }
protected function renderForm()
{
$this->loadAsset();
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = $action;
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
.'&configure='.$this->name.'&module_name='.$this->name;
$helper->currentIndex .= '&id_BarCodeGenerator='.(int)Tools::getValue('id_BarCodeGenerator');
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
public function getConfigFormValues(){
return array(
'prefixe2'=>Tools::getValue('prefixe2'),
'reference'=>Tools::getValue('reference'),
'key'=>Tools::getValue('key')
);
}
protected function getConfigForm(){
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Scan des codes barres'),
'icon' => 'icon-qrcode',
),
'input' => array(
array(
'col' => 3,
'type' => 'text',
//'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Entrez le prefix du code barre'),
'name' => 'prefixe2',
'label' => $this->l('Prefixe'),
'required'=>true
),
array(
'col' => 3,
'type' => 'text',
//'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Entrez la reférence de la commande'),
'name' => 'reference',
'label' => $this->l('Reférence commande'),
'required'=>true
),
array(
'col' => 3,
'type' => 'text',
//'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Entrez la clé du code barre'),
'name' => 'key',
'label' => $this->l('key'),
'required'=>true
),
),
'submit' => array(
'title' => $this->l('Mettre à jour le statut'),
),
),
);
}
}
but the shop i want to deploy it has the particularity the overrides are disabled, so i tried to insert the controller with two methods:
by trying to install modulesRoutes hook
public function hookModuleRoutes($params){
return [
'module-BarCodeGenerator-AdminBarCodeGeneratorAdmin'=>[
'controller'=>'AdminBarCodeGeneratorAdmin'
]
];
}
in this case, the hook did not even install successfully
-by adding manually the controller in the config/routes.yml
scanTab:
path: BarCodeGenerator/demo
methods: [GET]
defaults:
_controller: 'BarCodeGenerator/controllers/admin/AdminBarCodeGeneratorAdminController::initContent'
_legacy_controller: 'AdminBarCodeGeneratorAdminController'
_legacy_link: 'AdminBarCodeGeneratorAdminController'
but none of these methods worked

Related

Create form in back office by module Prestashop 1.7

I made a module to hook a form in product page in the back office (with hook DisplayAdminProductExtra).
How can I create a form with some inputs by module?
I think it can be done by {helper and .tpl file} or {form_field and .twig file}.
If anyone explains this as a walkthrough I'm sure it's gonna be a good reference for many others too.
this is the code that created by PrestaShop module generator:
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class Myfirstmodule extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'myfirstmodule';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'parsa';
$this->need_instance = 0;
/**
* Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
*/
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('new module');
$this->description = $this->l('first module');
$this->confirmUninstall = $this->l('Are you sure?');
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
}
/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
Configuration::updateValue('MYFIRSTMODULE_LIVE_MODE', false);
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('backOfficeHeader') &&
$this->registerHook('displayAdminProductsExtra');
}
public function uninstall()
{
Configuration::deleteByName('MYFIRSTMODULE_LIVE_MODE');
return parent::uninstall();
}
/**
* Load the configuration form
*/
public function getContent()
{
/**
* If values have been submitted in the form, process.
*/
if (((bool)Tools::isSubmit('submitMyfirstmoduleModule')) == true) {
$this->postProcess();
}
$this->context->smarty->assign('module_dir', $this->_path);
$output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');
return $output . $this->renderForm();
}
/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitMyfirstmoduleModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
. '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
/**
* Create the structure of your form.
*/
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->l('Live mode'),
'name' => 'MYFIRSTMODULE_LIVE_MODE',
'is_bool' => true,
'desc' => $this->l('Use this module in live mode'),
'values' => array(
array(
'id' => 'active_on',
'value' => true,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => false,
'label' => $this->l('Disabled')
)
),
),
array(
'col' => 3,
'type' => 'text',
'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Enter a valid email address'),
'name' => 'MYFIRSTMODULE_ACCOUNT_EMAIL',
'label' => $this->l('Email'),
),
array(
'type' => 'password',
'name' => 'MYFIRSTMODULE_ACCOUNT_PASSWORD',
'label' => $this->l('Password'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
/**
* Set values for the inputs.
*/
protected function getConfigFormValues()
{
return array(
'MYFIRSTMODULE_LIVE_MODE' => Configuration::get('MYFIRSTMODULE_LIVE_MODE', true),
'MYFIRSTMODULE_ACCOUNT_EMAIL' => Configuration::get('MYFIRSTMODULE_ACCOUNT_EMAIL', 'contact#prestashop.com'),
'MYFIRSTMODULE_ACCOUNT_PASSWORD' => Configuration::get('MYFIRSTMODULE_ACCOUNT_PASSWORD', null),
);
}
/**
* Save form data.
*/
protected function postProcess()
{
$form_values = $this->getConfigFormValues();
foreach (array_keys($form_values) as $key) {
Configuration::updateValue($key, Tools::getValue($key));
}
}
/**
* Add the CSS & JavaScript files you want to be loaded in the BO.
*/
public function hookBackOfficeHeader()
{
if (Tools::getValue('module_name') == $this->name) {
$this->context->controller->addJS($this->_path . 'views/js/back.js');
$this->context->controller->addCSS($this->_path . 'views/css/back.css');
}
}
/**
* Add the CSS & JavaScript files you want to be added on the FO.
*/
public function hookHeader()
{
$this->context->controller->addJS($this->_path . '/views/js/front.js');
$this->context->controller->addCSS($this->_path . '/views/css/front.css');
}
public function hookDisplayAdminProductsExtra()
{
/* Place your code here. */
}
}
Welcome on Stack Overflow
using HelperForm on PrestaShop 1.7 product page is not really working at this moment, it is recommended to use HTML markup and get all values of the form from the $_POST using actionProductSave hook

how to change a bool value in prestashop 1.6 back end by icon click

I want to change my printing status ,I am able to place the icon to change the status but the change is not occurring when I click the Print status icon shown in the image I have placed the callback as changePrintStatus
the code is reaching initProcess() when the change status icon is clicked,But after that what exactly must happen ??Or do i need to call any other function/override function ? (I want to do the same as in customers list where we change active status of customers ,similarly I want to do here for my custom module)
<?php
require_once(_PS_MODULE_DIR_.'eticketprinting/eticketprinting.php');
require_once(_PS_MODULE_DIR_.'eticketprinting/classes/Eticket.php');
class EticketController extends ModuleAdminController
{
public $module;
public $html;
public $tabName = 'renderForm';
public function __construct()
{
$this->tab = 'eticket';
$this->module = new eticketprinting();
$this->addRowAction('edit');
//$this->addRowAction('edit');
// $this->addRowAction('view'); // to display the view
//$this->addRowAction('delete');
$this->explicitSelect = false;
$this->context = Context::getContext();
$this->id_lang = $this->context->language->id;
$this->lang = false;
$this->ajax = 1;
$this->path = _MODULE_DIR_.'eticketprinting';
$this->default_form_language = $this->context->language->id;
$this->table = _DB_KITS_PREFIX_.'print_eticket';
$this->className = 'Eticket';
$this->identifier = 'id_print_eticket';
$this->allow_export = true;
$this->_select = '
id_print_eticket,
ticket_id,
ticket_no,
product_id,
print_status,
date_add,
date_upd
';
$this->name = 'EticketController';
$this->bootstrap = true;
$this->initTabModuleList();
$this->initToolbar();
$this->initPageHeaderToolbar();
// $this->initFieldList();
//$this->initContent();
parent::__construct();
$this->fields_list =
array(
'id_print_eticket' => array(
'title' => $this->l('E-Ticket Print ID'),
'width' => 25,
'type' => 'text',
),
'ticket_id' => array(
'title' => $this->l('Ticket- ID'),
'width' => 140,
'type' => 'text',
),
'ticket_no' => array(
'title' => $this->l('Ticket No'),
'width' => 140,
'type' => 'text',
),
'product_id' => array(
'title' => $this->l('Product ID'),
'width' => 100,
'type' => 'text',
),
'print_status' => array(
'title' => $this->l('Print Status'),
'align' => 'center',
'type' => 'bool',
'callback' => 'changePrintStatus',
'orderby' => false,
),
'date_add' => array(
'title' => $this->l('Date Add'),
'width' => 140,
'type' => 'text',
),
'date_upd' => array(
'title' => $this->l('Date Update'),
'width' => 140,
'type' => 'text',
),
);
}
public function changePrintStatus($value, $eticket)
{
return '<a class="list-action-enable '.($value ? 'action-enabled' : 'action-disabled').'" href="index.php?'.htmlspecialchars('tab=Eticket&id_print_eticket='
.(int)$eticket['id_print_eticket'].'&changePrintVal&token='.Tools::getAdminTokenLite('Eticket')).'">
'.($value ? '<i class="icon-check"></i>' : '<i class="icon-remove"></i>').
'</a>';
}
public function initProcess()
{
parent::initProcess();
//d($this->id_object);
if (Tools::isSubmit('changePrintVal') && $this->id_object) {
if ($this->tabAccess['edit'] === '1') {
//d("reached here");
$this->action = 'change_print_val';
} else {
$this->errors[] = Tools::displayError('You do not have permission to change this.');
}
}
}
public function postProcess()
{
//When generate pdf button is clicked
if (Tools::isSubmit('submitAddeticket')) {
if (!Validate::isDate(Tools::getValue('date_from'))) {
$this->errors[] = $this->l('Invalid "From" date');
}
if (!Validate::isDate(Tools::getValue('date_to'))) {
$this->errors[] = $this->l('Invalid "To" date');
}
if (!Validate::isInt(Tools::getValue('id_product')) || Tools::getValue('id_product')=='' ) {
$this->errors[] = $this->l('Invalid Product/select a product ');
}
if (!count($this->errors)) {
if (count(Ticket::getByProductNDateInterval(Tools::getValue('id_product'),Tools::getValue('date_from'), Tools::getValue('date_to')))) {
//d($this->context->link->getAdminLink('AdminPdf'));
Tools::redirectAdmin($this->context->link->getAdminLink('AdminPdf').'&submitAction=generateEticketPDF&id_product='.urlencode(Tools::getValue('id_product')).'&date_from='.urlencode(Tools::getValue('date_from')).'&date_to='.urlencode(Tools::getValue('date_to')));
}
$this->errors[] = $this->l('No tickets has been found or Ticket Generated Already for this period for Product ID:'.Tools::getValue('id_product').' (Change the Print Status generate the E-Ticket Again)');
}
} else {
parent::postProcess();
}
}
}
I was missing the process function :
**But note that $this->action = 'change_print_val';
and name f the process must be similar in my case processChangePrintVal
So if action is $this->action = 'change_printStatus_val'; then process name must be processChangePrintStatusVal**
I added Below function
/**
* Toggle the Eticket Print Status flag- Here the update action occurs
*/
public function processChangePrintVal()
{
$eticket = new Eticket($this->id_object);
if (!Validate::isLoadedObject($eticket)) {
$this->errors[] = Tools::displayError('An error occurred while updating Eticket Print Status information.');
}
$eticket->print_status = $eticket->print_status ? 0 : 1;
if (!$eticket->update()) {
$this->errors[] = Tools::displayError('An error occurred while Eticket Print Status customer information.');
}
Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
}

Additional fields in Prestashop module - changes not displaing, parse error & class missing

I'm usually working with WP, but I need to manipulate a module blockcontactinfos on Prestashop - just simply add two more fields which will be shown on the front end, but somehow it's not working.
I have carrefully copied one of the fields, changed it everywhere but I when trying to clear the cache (in the Performance menu):
2 errors
blockcontactinfos (parse error in /modules/blockcontactinfos/blockcontactinfos.php)
blockcontactinfos (class missing in /modules/blockcontactinfos/blockcontactinfos.php)
Could anybody help me out of this? Thanks a lot in advance. Prestashop is 1.6. Fields are displayed correctly in settings, values saved, but there is the error above and I just can't force the web page to load changed template file.
blockcontactinfos.php (added the ones with _url), lines 31-141, changes marked with // KV:
<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;
class Blockcontactinfos extends Module
{
protected static $contact_fields = array(
'BLOCKCONTACTINFOS_COMPANY',
'BLOCKCONTACTINFOS_ADDRESS',
'BLOCKCONTACTINFOS_ADDRESS_URL',
'BLOCKCONTACTINFOS_PHONE',
'BLOCKCONTACTINFOS_PHONE_URL',
'BLOCKCONTACTINFOS_EMAIL',
);
public function __construct()
{
$this->name = 'blockcontactinfos';
$this->author = 'PrestaShop';
$this->tab = 'front_office_features';
$this->version = '1.2.0';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Contact information block');
$this->description = $this->l('This module will allow you to display your e-store\'s contact information in a customizable block.');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
Configuration::updateValue('BLOCKCONTACTINFOS_COMPANY', Configuration::get('PS_SHOP_NAME'));
Configuration::updateValue('BLOCKCONTACTINFOS_ADDRESS', trim(preg_replace('/ +/', ' ', Configuration::get('PS_SHOP_ADDR1').' '.Configuration::get('PS_SHOP_ADDR2')."\n".Configuration::get('PS_SHOP_CODE').' '.Configuration::get('PS_SHOP_CITY')."\n".Country::getNameById(Configuration::get('PS_LANG_DEFAULT'), Configuration::get('PS_SHOP_COUNTRY_ID')))));
Configuration::updateValue('BLOCKCONTACTINFOS_ADDRESS_URL', Configuration::get('PS_SHOP_ADDRESS_URL'));
Configuration::updateValue('BLOCKCONTACTINFOS_PHONE', Configuration::get('PS_SHOP_PHONE'));
Configuration::updateValue('BLOCKCONTACTINFOS_PHONE_URL', Configuration::get('PS_SHOP_PHONE_URL'));
Configuration::updateValue('BLOCKCONTACTINFOS_EMAIL', Configuration::get('PS_SHOP_EMAIL'));
$this->_clearCache('blockcontactinfos.tpl');
return (parent::install() && $this->registerHook('header') && $this->registerHook('footer'));
}
public function uninstall()
{
foreach (Blockcontactinfos::$contact_fields as $field)
Configuration::deleteByName($field);
return (parent::uninstall());
}
public function getContent()
{
$html = '';
if (Tools::isSubmit('submitModule'))
{
foreach (Blockcontactinfos::$contact_fields as $field)
Configuration::updateValue($field, Tools::getValue($field));
$this->_clearCache('blockcontactinfos.tpl');
$html = $this->displayConfirmation($this->l('Configuration updated'));
}
return $html.$this->renderForm();
}
public function hookHeader()
{
$this->context->controller->addCSS(($this->_path).'blockcontactinfos.css', 'all');
}
public function hookFooter($params)
{
if (!$this->isCached('blockcontactinfos.tpl', $this->getCacheId()))
foreach (Blockcontactinfos::$contact_fields as $field)
$this->smarty->assign(strtolower($field), Configuration::get($field));
return $this->display(__FILE__, 'blockcontactinfos.tpl', $this->getCacheId());
}
public function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Company name'),
'name' => 'BLOCKCONTACTINFOS_COMPANY',
),
array(
'type' => 'textarea',
'label' => $this->l('Address'),
'name' => 'BLOCKCONTACTINFOS_ADDRESS',
),
array(
'type' => 'text',
'label' => $this->l('URL na Google mapy'),
'name' => 'BLOCKCONTACTINFOS_ADDRESS_URL',
),
array(
'type' => 'text',
'label' => $this->l('Phone number'),
'name' => 'BLOCKCONTACTINFOS_PHONE',
),
array(
'type' => 'text',
'label' => $this->l('Telefonní číslo bez mezer'),
'name' => 'BLOCKCONTACTINFOS_PHONE_URL',
),
array(
'type' => 'text',
'label' => $this->l('Email'),
'name' => 'BLOCKCONTACTINFOS_EMAIL',
),
),
'submit' => array(
'title' => $this->l('Save')
)
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => array(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
foreach (Blockcontactinfos::$contact_fields as $field)
$helper->tpl_vars['fields_value'][$field] = Tools::getValue($field, Configuration::get($field));
return $helper->generateForm(array($fields_form));
}
}
blockcontactinfos.tpl (added the ones with _url), lines 32-33:
{if $blockcontactinfos_address != ''}<li><pre> {$blockcontactinfos_address|escape:'html':'UTF-8'|nl2br}</pre></li>{/if}
{if $blockcontactinfos_phone != ''}<li>{l s='Tel' mod='blockcontactinfos'} {$blockcontactinfos_phone|escape:'html':'UTF-8'}</li>{/if}

Back-office tab and helper form?

Following this prestashop instruction on how to make a tab in a back-office I did a class and controller like it's need. But what if I want to use a helper form for form creation in the AdminTest controller?
class AdminTest extends AdminTab
{
public function __construct()
{
$this->table = 'test';
$this->className = 'Test';
$this->lang = false;
$this->edit = true;
$this->delete = true;
$this->fieldsDisplay = array(
'id_test' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 25),
'test' => array(
'title' => $this->l('Name'),
'width' => 200)
);
$this->identifier = 'id_test';
parent::__construct();
}
public function displayForm()
{
global $currentIndex;
$defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
$languages = Language::getLanguages();
$obj = $this->loadObject(true);
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Edit carrier'),
'image' => '../img/admin/icon_to_display.gif'
),
'input' => array(
array(
'type' => 'text',
'name' => 'shipping_method',
),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
return $helper->generateForm($fields_form);
}
}
But it wont work. Why the helper form isn't working?
p.s. btw, also I would like to use a $this->setTemplate('mytemplate.tpl') method but it's not possible aswell.
To create a new Admin tab, I prefer to use a module in which just install a tab on Module installation.
I think it's prettier, and more easily exportable to other website.
We can use this way : Templates, Helpers ...
For exemple :
Create in your module this dir : controllers/admin
Create a new class in previous created dir adminTest.php :
class AdminTestController extends ModuleAdminController {
}
In this class, you can override all ModuleAdminController functions and use templates, helpers (look in the class AdminController)
Now in your Module :
class testModule extends Module {
public function __construct() {
$this->name = 'testmodule';
$this->tab = 'administration';
$this->version = '1.0';
$this->author = 'You';
$this->need_instance = 1;
$this->secure_key = Tools::encrypt($this->name);
parent::__construct();
$this->displayName = $this->l('Admin Test Tab Module');
$this->description = $this->l('Add a new Admin Tab in BO');
}
public function install() {
return parent::install() &&
$this->_installTab();
}
public function uninstall() {
return $this->_unInstallTabs() &&
parent::uninstall();
}
private function _installTabs() {
if (!$AdminTestId = Tab::getIdFromClassName('AdminTest')):
$tab = new Tab();
$tab->class_name = 'AdminTest';
$tab->module = $this->name;
$tab->id_parent = Tab::getIdFromClassName('AdminParentOrders'); // Under Orders Tab, To add a new Tab on First level like Orders/Customers... put 0
$tab->active = 1;
foreach (Language::getLanguages(false) as $lang):
$tab->name[(int) $lang['id_lang']] = 'Admin Test';
endforeach;
if (!$tab->save()):
return $this->_abortInstall($this->l('Unable to create the "Admin Test" tab'));
endif;
else:
$AdminTest = new Tab((int) $AdminTestId);
endif;
}
// Uninstall Tabs on Module uninstall
private function _unInstallTabs() {
// Delete the Module Back-office tab
if ($id_tab = Tab::getIdFromClassName('AdminTest')) {
$tab = new Tab((int) $id_tab);
$tab->delete();
return true;
}
}
So, when you Install your Module, a new tab is available, and you can do what you want in your AdminTestController like a real Admin Controller

Prestashop - Translatable form field fails with HelperForm

I'm trying to do a Prestashop module, but when trying to set a translatable form field at the configuration form, it fails.
This is the error I get, through the JS console:
[14:33:39.915] ReferenceError: defaultLanguage is not defined # http://localhost:8888/js/admin.js:173
I think that I have well configurated the languajes in the backoffice, so I'm not sure why is this happening.
This is how I try to create the form:
public function displayForm()
{
// Get default Language
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
// Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Título de la noticia'),
'name' => 'NOTICIA_TIT',
'size' => 30,
'required' => true,
'lang' => true
),
array(
'type' => 'text',
'label' => $this->l('Imagen de la noticia'),
'name' => 'NOTICIA_IMG',
'size' => 30,
'required' => true,
'enabled' => false
),
array(
'type' => 'file',
'label' => $this->l('Subir nueva imagen'),
'name' => 'NOTICIA_IMG_FILE',
'size' => 30
),
array(
'type' => 'textarea',
'label' => $this->l('Texto de la noticia'),
'name' => 'NOTICIA_TXT',
'required' => true,
'cols' => 30,
'rows' => 4,
'lang' => true
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
// Load current value
$helper->fields_value['NOTICIA_TXT'] = Configuration::get('NOTICIA_TXT');
$helper->fields_value['NOTICIA_TIT'] = Configuration::get('NOTICIA_TIT');
$helper->fields_value['NOTICIA_IMG'] = Configuration::get('NOTICIA_IMG');
return $helper->generateForm($fields_form);
}
EDIT: I've seen this in the code.
<script type="text/javascript">
var module_dir = '/modules/';
var id_language = 1;
var languages = new Array();
var vat_number = 1;
// Multilang field setup must happen before document is ready so that calls to displayFlags() to avoid
// precedence conflicts with other document.ready() blocks
// we need allowEmployeeFormLang var in ajax request
allowEmployeeFormLang = 1;
displayFlags(languages, id_language, allowEmployeeFormLang);
$(document).ready(function() {
if ($(".datepicker").length > 0)
$(".datepicker").datepicker({
prevText: '',
nextText: '',
dateFormat: 'yy-mm-dd'
});
});
</script>
languajes variable is created as an empty array. However, this is function displayFlags:
function displayFlags(languages, defaultLanguageID, employee_cookie)
{
if ($('.translatable'))
{
$('.translatable').each(function() {
if (!$(this).find('.displayed_flag').length > 0) {
$.each(languages, function(key, language) {
if (language['id_lang'] == defaultLanguageID)
{
defaultLanguage = language;
return false;
}
});
var displayFlags = $('<div></div>')
.addClass('displayed_flag')
.append($('<img>')
.addClass('language_current')
.addClass('pointer')
.attr('src', '../img/l/' + defaultLanguage['id_lang'] + '.jpg')
.attr('alt', defaultLanguage['name'])
.click(function() {
toggleLanguageFlags(this);
})
);
var languagesFlags = $('<div></div>')
.addClass('language_flags')
.html('Choose language:<br /><br />');
$.each(languages, function(key, language) {
var img = $('<img>')
.addClass('pointer')
.css('margin', '0 2px')
.attr('src', '../img/l/' + language['id_lang'] + '.jpg')
.attr('alt', language['name'])
.click(function() {
changeFormLanguage(language['id_lang'], language['iso_code'], employee_cookie);
});
languagesFlags.append(img);
});
if ($(this).find('p:last-child').hasClass('clear'))
$(this).find('p:last-child').before(displayFlags).before(languagesFlags);
else
$(this).append(displayFlags).append(languagesFlags);
}
});
}
}
I fix the same error that you got but I get another one.
In order to define defaultLanguage, you should fill languages attribute of the helperform. You can do it this way:
$languages = Language::getLanguages(true);
$helper->languages = $languages;
I'm not sure whether you should put true or false for getLanguages... I tired both and I still get this error :
Uncaught SyntaxError: Unexpected token ILLEGAL
It happens here:
languages[0] = {
id_lang: 1,
iso_code: 'en',
name: 'English',
is_default: '<br />
So now, there probably more to do in order to have the property is_default defined... Have you find a way fix your problem?
EDIT :
By setting is_default on your own, it works. But it's ugly...
// Languages
$languages = Language::getLanguages(true);
for($i=0; $i<count($languages); $i++){
if($languages[$i]['id_lang'] == $default_lang){
$languages[$i]['is_default'] = 1;
}else{
$languages[$i]['is_default'] = 0;
}
}
$helper->languages = $languages;
Get the languages from the module controller:
$languages = $this->context->controller->getLanguages();