Create form in back office by module Prestashop 1.7 - prestashop

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

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

Prestashop : Can't make my module work : Get order details when order status is changed to “shipped”

I am developing a module, it gets order details when order status is changed to "shipped" by admin, to post them to a third-party application with API.
I am using Prestashop V 1.7.7.0.
The hook i'm using is hookActionOrderStatusPostUpdate
The module is installed successfully in the Backoffice and there's no syntax errors, But it doesn't do anything.
Can't figure out what's wrong in my code.
Need help please. Thanks
<?php
/**
* 2007-2021 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license#prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* #author PrestaShop SA <contact#prestashop.com>
* #copyright 2007-2021 PrestaShop SA
* #license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class Cargo extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'cargo';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'tekwave';
$this->need_instance = 1;
/**
* Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
*/
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('cargo');
$this->description = $this->l('Ce module permet la synchronisation entre Prestashop et Cargo');
$this->confirmUninstall = $this->l('');
$this->ps_versions_compliancy = array('min' => '1.6', '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('CARGO_LIVE_MODE', false);
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('backOfficeHeader') &&
$this->registerHook('actionOrderStatusPostUpdate');
}
public function uninstall()
{
Configuration::deleteByName('CARGO_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('submitCargoModule')) == 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 = 'submitCargoModule';
$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' => 'CARGO_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' => 'CARGO_ACCOUNT_EMAIL',
'label' => $this->l('Email'),
),
array(
'type' => 'password',
'name' => 'CARGO_ACCOUNT_PASSWORD',
'label' => $this->l('Password'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
/**
* Set values for the inputs.
*/
protected function getConfigFormValues()
{
return array(
'CARGO_LIVE_MODE' => Configuration::get('CARGO_LIVE_MODE', true),
'CARGO_ACCOUNT_EMAIL' => Configuration::get('CARGO_ACCOUNT_EMAIL', 'contact#prestashop.com'),
'CARGO_ACCOUNT_PASSWORD' => Configuration::get('CARGO_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');
}
This is my code under hookActionOrderStatusPostUpdate function :
public function hookActionOrderStatusPostUpdate($params)
{
if ($params['newOrderStatus']->id == 6) {
$order = new Order((int)$params['id_order']);
$address = new Address((int)$order->id_address_delivery);
$country = new Country((int)($address->id_country));
$state = new State((int)($address->id_state));
$message = new Message((int)($order->id_message));
$Products = $order->getProducts();
$tel_cl = $address->phone;
$nom_prenom_cl = $address->lastname . ' ' . $address->firstname;
$ville_cl = $country->name;
$delegation_cl = $state->name;
$adresse_cl = $address->address1 . ' ' . $address->address2;
$tel_2_cl = $address->phone_mobile;
$libelle = '';
$nb_piece = 0;
foreach ($Products as $product) {
$libelle .= $product['product_name'] . '<br/>';
$nb_piece += $product['product_quantity'];
}
$cod = $order->total_paid;
$remarque = $message->message;
$Url_str = 'http://admin.cargotunisie.com/cargo_api/set_colis_cargo_get.php?id=1&tel_cl='.$tel_cl.'&nom_prenom_cl='.$nom_prenom_cl.'&ville_cl='.$ville_cl.'&delegation_cl='.$delegation_cl.'&adresse_cl='.$adresse_cl.'&tel_2_cl='.$tel_2_cl.'&libelle='.$libelle.'&cod='.$cod.'&nb_piece='.$nb_piece.'&remarque='.$remarque;
$json = Tools::file_get_contents($Url_str);
$result = json_decode($json);
}
}
}
It could be due to several things.
First: make sure the hook is hooked.
Go to Design > Positions > Then add new hook
Choose your module's name and check if you can select the hook actionOrderStatusPostUpdate. If you can, then it means it was unhooked.
This happens more than we would like, and sometimes you can get crazy until you found it
If that doesn't work you can try this (only if is not on a production site). If it is, just wrap it by using an if that checks if it's from your IP.
Add Tools::dieObject($params); at the beginning of the function.
Then change the an order and check the results. You should see a user friendly list of the values stored in $params.
Make it sure the $params['newOrderStatus'] exists and $params['newOrderStatus']->id is an object.
Also, it should be pretty obvious, but make sure the order ID which are you doing the tests is 6.
With that you should be able to have a general clue of what may be happening.

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

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}

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);
}
}