Prestashop 1.7 custom module front controller doesn't work - prestashop

Trying to create a front controller module that has a strange behaviour
Using the $link->getModuleLink('vpages', 'dpage', $params) in the tpl page, the URL will be correctly presented as mysite.com/en/40-flower-delivery-Andorra.html. When trying to access the page, I will be provided with a 404 page and the url will become mysite.com/en/index.php?controller=dpage&id_country=40&country=Andorra&module=vpages
if I manually alter the url by adding the &fc=module, I will get a 500 Internal error page
The module has the following script:
<?php
class vPages extends Module
{
private $html = '';
private $postErrors = array();
public function __construct()
{
$this->name = 'vpages';
$this->tab = 'others';
$this->version = '1.0';
$this->author = 'Rosu Andrei Mihai';
$this->is_eu_compatible = 1;
$this->need_instance = 1;
$this->bootstrap = true;
$this->controllers = array('dpage');
$this->displayName = $this->l('vPages');
$this->description = $this->l('Virtual dynamic pages');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->confirmUninstall = $this->l('Are you sure you want to delete all saved details?');
$this->secure_key = Tools::encrypt($this->name);
parent::__construct();
}
public function install()
{
return parent::install() && $this->registerHook('moduleRoutes');
}
public function hookModuleRoutes($params){
$my_link = array(
'module-vpages-dpage' => array(
'controller' => 'dpage',
'rule' => '{id_country:-}flower-delivery{-:country}{-:city}.html',
'keywords' => array(
'id_country' => array('regexp' => '[0-9]+', 'param' => 'id_country'),
'setCountry' => array('regexp' => '[0-9]+', 'param' => 'setCountry'),
'country' => array('regexp' => '[_a-zA-Z0-9\pL\pS]+', 'param' => 'country'),
'city' => array('regexp' => '[_a-zA-Z0-9\w\pL\pS-]*', 'param' => 'city'),
'module_action' => array('regexp' => '[\w]+', 'param' => 'module_action')
),
'params' => array(
'fc' => 'module',
'module' => 'vpages'
)
)
);
return $my_link;
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
return true;
}
}
The front controller has the following script:
<?php
class vpagesdpageModuleFrontController extends ModuleFrontController
{
public $errors = array();
public $display_column_left = false;
public $display_column_right = false;
public $ssl = true;
public $php_self = 'dpage';
public function __construct()
{
parent::__construct();
$this->page_name = 'dpage';
$this->context = Context::getContext();
}
public function initContent()
{
parent::initContent();
$this->context->cookie->__set('productbycountry_id',Tools::getValue('id_country'));
$sql = "SELECT DISTINCT ps_country_lang.id_country, ps_country_lang.name as country, city FROM ps_cms_target_world
LEFT JOIN ps_country_lang ON ps_cms_target_world.id_country = ps_country_lang.id_country
WHERE ps_country_lang.id_country=" . Tools::getValue('id_country') . "
ORDER BY country ASC";
//die($sql);
$cPages = Db::getInstance()->ExecuteS($sql);
$get_url = Db::getInstance()->ExecuteS('SELECT domain,physical_uri FROM '._DB_PREFIX_.'shop_url ');
$protocol = (isset($_SERVER['HTTPS']) ? "https" : "http") ;
$site_url = "$protocol://".$get_url[0]['domain'].$get_url[0]['physical_uri']."modules";
$controller_url = $this->context->link->getModuleLink('vpages', 'vPagesController', array(), true);
$this->context->smarty->assign(array(
'link' => $this->context->link,
'controller_url' => $controller_url,
'cPages' => $cPages,
'SITEURL' => $site_url
));
$country = urldecode(Tools::getValue('country'));
$city = urldecode(Tools::getValue('city'));
if(isset($city) && !empty($city)) $title = " - " . $city;
$this->context->smarty->assign('meta_title', 'Flower delivery to ' . $country . $title);
$this->setTemplate('module:vpages/views/templates/front/dPage.tpl');
}
}
In the tpl file, I generate the friendly url using the following script:
{foreach $vPages as $page}
<div class="country_content col-xs-4 col-sm-3 col-md-2 center-block text-center">
<p>
{assign var=params value=[
'module_action' => 'list',
'id_country'=> $page.id_country,
'setCountry'=> $page.id_country,
'country'=> urlencode($page.country),
'city' => null
]}
{assign var="meta_title" value="Flowers to {$page.country}"}
<a style="color:normal" href="{$link->getModuleLink('vpages', 'dpage', $params)|escape:'html':'UTF-8'}" title="{$meta_title|escape:'html':'UTF-8'}">
<img width="98" height="70" src="{$SITEURL}/vpages/flags/{$page.country}.png" style="padding-right: 3px; margin=bottom: 5px;" />
<br />
<p>{$page.country|escape:'html':'UTF-8'}</p>
</a>
</p>
</div>
{/foreach}
I don't get what is wrong in the code, in PS 1.6 the code works perfect in PS 1.7 it doesn't
Could someone point me to the mistake?
Thanks!

The link should be created in your module's class not in the tpl, a tpl is just for layout.
Use this in your class $this->context->link->getModuleLink('your_module_name', 'ajax', [], null, null, null, true)
Then assign it with smarty to your tpl.

Related

how to add an admin tab without extending a controller in 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

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

Prestashop 1.7 - Overriding module class

i try to overriding a module in my custom themes, so I have copy past the original folder (from module folder in my themes > themename > modules folder)
but its not working
I have in my module folder from my theme :
ps_sharebuttons > views
and
ps_sharebuttons > ps_sharebuttons.php
which contains
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class Ps_SharebuttonsOverride extends Ps_Sharebuttons
{
public function renderWidget($hookName, array $params)
{
var_dump($params);
exit;
$key = 'ps_sharebuttons|' . $params['product']['id_product'];
if (!empty($params['product']['id_product_attribute'])) {
$key .= '|' . $params['product']['id_product_attribute'];
}
if (!$this->isCached($this->templateFile, $this->getCacheId($key))) {
$this->smarty->assign($this->getWidgetVariables($hookName, $params));
}
return $this->fetch($this->templateFile, $this->getCacheId($key));
}
public function getWidgetVariables($hookName, array $params)
{
if (!isset($this->context->controller->php_self) || $this->context->controller->php_self != 'product') {
return;
}
$product = $this->context->controller->getProduct();
if (!Validate::isLoadedObject($product)) {
return;
}
$social_share_links = [];
$sharing_url = addcslashes($this->context->link->getProductLink($product), "'");
$sharing_name = addcslashes($product->name, "'");
$image_cover_id = $product->getCover($product->id);
if (is_array($image_cover_id) && isset($image_cover_id['id_image'])) {
$image_cover_id = (int)$image_cover_id['id_image'];
} else {
$image_cover_id = 0;
}
$sharing_img = addcslashes($this->context->link->getImageLink($product->link_rewrite, $image_cover_id), "'");
if (Configuration::get('PS_SC_FACEBOOK')) {
$social_share_links['facebook'] = array(
'label' => $this->trans('Share', array(), 'Modules.Sharebuttons.Shop'),
'class' => 'facebook',
'url' => 'http://www.facebook.com/sharer.php?u='.$sharing_url,
);
}
if (Configuration::get('PS_SC_TWITTER')) {
$social_share_links['twitter'] = array(
'label' => $this->trans('Tweet', array(), 'Modules.Sharebuttons.Shop'),
'class' => 'twitter',
'url' => 'https://twitter.com/intent/tweet?text='.$sharing_name.' '.$sharing_url,
);
}
if (Configuration::get('PS_SC_GOOGLE')) {
$social_share_links['googleplus'] = array(
'label' => $this->trans('Google+', array(), 'Modules.Sharebuttons.Shop'),
'class' => 'googleplus',
'url' => 'https://plus.google.com/share?url='.$sharing_url,
);
}
if (Configuration::get('PS_SC_PINTEREST')) {
$social_share_links['pinterest'] = array(
'label' => $this->trans('Pinterest', array(), 'Modules.Sharebuttons.Shop'),
'class' => 'pinterest',
'url' => 'http://www.pinterest.com/pin/create/button/?media='.$sharing_img.'&url='.$sharing_url,
);
}
return array(
'social_share_links' => $social_share_links,
);
}
}
But prestashop still use the orignal file module because i get this error :
'Undefined index: product', '/home/xxxxxxxx/www/modules/ps_sharebuttons/ps_sharebuttons.php',
I've already clear cache
Thanks for help
Ok im stupid, the override view goes to theme > module folder but the class should be in override > module folder

Very basic "creating your first module" tutorial fail. How?

So I'm following Prestashop's published hand-holding guide (which references their helper classes), but it isn't working.
Since all the other "how to make a module" questions just point there, I'm getting nowhere fast.
At the bottom of this question is the code where I'm at so far and I don't get a particular error, but I'm not seeing the * as promised beside the required field. When I add
'desc' => $this->l('Description displayed under the field.'),
It goes to the left, not under, and if I add
'lang' => true,
I lose the input entirely.
That's all copy pasted out of their guides, so what am I missing? Unfortunately it isn't a very complete document. They really should have a complete working example. Now, there is reference vaguely to a template file (tpl) but so far up to this point it isn't explicitly mentioned. So at this point I only have the single php file indicated up to this point in the tutorial.
<?php
if (!defined('_PS_VERSION_'))
exit;
class SPFishbox extends Module
{
public function __construct()
{
$this->name = 'spfishbox';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'SP';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = false;
parent::__construct();
$this->displayName = $this->l('Fishbox');
$this->description = $this->l('Adds Fishbox code snipped.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('MYMODULE_NAME'))
$this->warning = $this->l('No name provided');
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install() ||
!$this->registerHook('header') ||
!Configuration::updateValue('MYMODULE_NAME', 'Fishbox Snippet')
)
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall())
return false;
return true;
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit'.$this->name))
{
$my_module_name = strval(Tools::getValue('MYMODULE_NAME'));
if (!$my_module_name
|| empty($my_module_name)
|| !Validate::isGenericName($my_module_name))
$output .= $this->displayError($this->l('Invalid Configuration value'));
else
{
Configuration::updateValue('MYMODULE_NAME', $my_module_name);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output.$this->displayForm();
}
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('Configuration value'),
'name' => 'MYMODULE_NAME',
'size' => 20,
'required' => 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['MYMODULE_NAME'] = Configuration::get('MYMODULE_NAME');
return $helper->generateForm($fields_form);
}
}
You have to do the following modifications:
$this->bootstrap = true;
and:
$languages = Language::getLanguages(false);
foreach ($languages as $k => $language)
$languages[$k]['is_default'] = (int)$language['id_lang'] == Configuration::get('PS_LANG_DEFAULT');
$helper->languages = $languages;
Full code:
<?php
if (!defined('_PS_VERSION_'))
exit;
class SPFishbox extends Module
{
public function __construct()
{
$this->name = 'spfishbox';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'SP';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Fishbox');
$this->description = $this->l('Adds Fishbox code snipped.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('MYMODULE_NAME'))
$this->warning = $this->l('No name provided');
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install() ||
!$this->registerHook('header') ||
!Configuration::updateValue('MYMODULE_NAME', 'Fishbox Snippet')
)
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall())
return false;
return true;
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit'.$this->name))
{
$my_module_name = strval(Tools::getValue('MYMODULE_NAME'));
if (!$my_module_name
|| empty($my_module_name)
|| !Validate::isGenericName($my_module_name))
$output .= $this->displayError($this->l('Invalid Configuration value'));
else
{
Configuration::updateValue('MYMODULE_NAME', $my_module_name);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output.$this->displayForm();
}
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('Configuration value'),
'name' => 'MYMODULE_NAME',
'size' => 20,
'required' => true,
'desc' => $this->l('Description displayed under the field.'),
'lang' => true,
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
$helper = new HelperForm();
$languages = Language::getLanguages(false);
foreach ($languages as $k => $language)
$languages[$k]['is_default'] = (int)$language['id_lang'] == Configuration::get('PS_LANG_DEFAULT');
$helper->languages = $languages;
// 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['MYMODULE_NAME'] = Configuration::get('MYMODULE_NAME');
return $helper->generateForm($fields_form);
}
}

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