...
That class must bear the same name as the module and its folder, in CamelCase: MyModule
.
Furthermore, that class must extend the Module
class, and thus inherits all methods and attributes. It can just as well extend any class derived from Module
: PaymentModule
, ModuleGridEngine
, ModuleGraph
...
Code Block |
---|
borderStyle | solid |
---|
title | mymodule.php |
---|
borderStyle | solid |
---|
|
<?php
if (!defined('_PS_VERSION_'))
exit;
class MyModule extends Module
{
public function __construct()
{
$this->name = 'mymodule';
$this->tab = 'Test';
$this->version = 1.0;
$this->author = 'Firstname Lastname';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('My module');
$this->description = $this->l('Description of my module.');
}
public function install()
{
if (parent::install() == false)
return false;
return true;
}
}
?>
|
...
To that effect, we are going to change your module's code, and add these lines:
Code Block |
---|
borderStyle | solid |
---|
title | mymodule.php (partial) |
---|
borderStyle | solid |
---|
|
public function install()
{
if (parent::install() == false OR !$this->registerHook('leftColumn'))
return false;
return true;
}
...
public function hookLeftColumn( $params )
{
global $smarty;
return $this->display(__FILE__,'mymodule.tpl');
}
public function hookRightColumn($params)
{
return $this->hookLeftColumn($params);
}
|
...
So, let's create the mymodule.tpl
file, and add some lines of code to it.
Code Block |
---|
borderStyle | solid |
---|
title | mymodule.tpl |
---|
borderStyle | solid |
---|
|
<!-- Block mymodule -->
<div id="mymodule_block_left" class="block">
<h4>Welcome!</h4>
<div class="block_content">
<ul>
<li><a href="{$base_dir}modules/mymodule/mymodule_page.php" title="Click this link">Click me!</a></li>
</ul>
</div>
</div>
<!-- /Block mymodule -->
|
...
Tip |
---|
You should strive to use explicit and recognizable names for your TPL files, so that you can find them quickly in the back-office – which is a must when using the translation tool. |
Code Block |
---|
borderStyle | solid |
---|
title | mymodule_page.tpl |
---|
borderStyle | solid |
---|
|
Welcome to my shop!
|
Code Block |
---|
borderStyle | solid |
---|
title | mymodule_page.php |
---|
borderStyle | solid |
---|
|
<?php
global $smarty;
include('../../config/config.inc.php');
include('../../header.php');
$smarty->display(dirname(__FILE__).'/mymodule_page.tpl');
include('../../footer.php');
?>
|
...
For instance, in our mymodule_page.php
, we can create such a variable:
Code Block |
---|
borderStyle | solid |
---|
title | mymodule_page.php |
---|
borderStyle | solid |
---|
|
<?php
global $smarty;
include('../../config/config.inc.php');
include('../../header.php');
$mymodule = new MyModule();
$message = $mymodule->l('Welcome to my shop!');
$smarty->assign('messageSmarty', $message); // creation of our variable
$smarty->display(dirname(__FILE__).'/mymodule_page.tpl');
include( '../../footer.php' );
?>
|
From there on, we can ask Smarty to display the content of this variable in our TPL file.
Code Block |
---|
borderStyle | solid |
---|
title | mymodule_page.tpl |
---|
borderStyle | solid |
---|
|
{$messageSmarty}
|
PrestaShop includes a number of variables. For instance, {$HOOK_LEFT_COLUMN} will be replaced with the content for the left column, meaning the content from all the modules that have been attached to the left column's hook.
...
Strings in PHP files will need to be displayed through the l()
method, from the Module.php
abstract class.
Code Block |
---|
borderStyle | solid |
---|
title | mymodule.php (partial) |
---|
borderStyle | solid |
---|
|
...
$this->displayName = $this->l('My module');
$this->description = $this->l('Description of my module.');
...
|
Strings in TPL files will need to be turned into dynamic content, which Smarty will replace by the translation for the chosen language. In our sample module, this file:
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | mymodule.tpl (partial) |
---|
borderStyle | solid |
---|
xml |
|
<li>
<a href="{$base_dir}modules/mymodule/mymodule_page.php" title="Click this link">Click me!</a>
</li>
|
...becomes:
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | mymodule.tpl (partial) |
---|
borderStyle | solid |
---|
xml |
|
<li>
<a href="{$base_dir}modules/mymodule/mymodule_page.php" title="{l s='Click this link' mod='mymodule'}">{l s='Click me!' mod='mymodule'}</a>
</li>
|
...and this one:
Code Block |
---|
| xml |
---|
| xml |
---|
title | mymodule_page.tpl | xml |
---|
|
<h4>Welcome!</h4>
...
Click me!
|
...becomes:
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | mymodule.tpl |
---|
borderStyle | solid |
---|
xml |
|
<h4>{l s='Welcome!' mod='mymodule'}</h4>
...
{l s='Click me!' mod='mymodule'}
|
...
The translations are saved in a new file, fr.php
(or languageCode.php
, which is generated by PrestaShop and looks like this:
Code Block |
---|
borderStyle | solid |
---|
title | mymodule.tpl |
---|
borderStyle | solid |
---|
|
<?php
global $_MODULE;
$_MODULE = array();
$_MODULE['<{mymodule}prestashop>mymodule_2ddddc2a736e4128ce1cdfd22b041e7f'] = 'Mon module';
$_MODULE['<{mymodule}prestashop>mymodule_d6968577f69f08c93c209bd8b6b3d4d5'] = 'Description de mon module';
$_MODULE['<{mymodule}prestashop>mymodule_c66b10fbf9cb6526d0f7d7a602a09b75'] = 'Cliquez sur ce lien';
$_MODULE['<{mymodule}prestashop>mymodule_f42c5e677c97b2167e7e6b1e0028ec6d'] = 'Cliquez-moi \!';
$_MODULE['<{mymodule}prestashop>mymodule_page_c0d7cffa0105851272f83d5c1fe63a1c'] = 'Bienvenue dans ma boutique \!';
|
...
- Add a new table to your PrestaShop database, named
ps_test
. Give it two fields:id_test
(INT 11)test
(VARCHAR 32)
- Create a blank file named
Test.php
in PrestaShop's /classes
folder. - Add the following lines to that file:
Code Block |
---|
borderStyle | solid |
---|
title | Test.php |
---|
borderStyle | solid |
---|
|
<?php
class Test extends ObjectModel
{
/** @var string Name */
public $test;
protected $fieldsRequired = array('test');
protected $fieldsSize = array('test' => 64);
protected $fieldsValidate = array('test' => 'isGenericName');
protected $table = 'test';
protected $identifier = 'id_test';
public function getFields()
{
parent::validateFields();
$fields['test'] = pSQL($this->test);
return $fields;
}
}
?>
|
- Create a blank file named
AdminTest.php
in PrestaShop's /admin/tabs
. - Add the following lines to that file:
Code Block |
---|
borderStyle | solid |
---|
title | AdminTest.php |
---|
borderStyle | solid |
---|
|
<?php
include_once( PS_ADMIN_DIR.'/../classes/AdminTab.php');
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);
echo '
<script type="text/javascript">
id_language = Number('.$defaultLanguage.');
</script>';
echo '
<form action="' . $currentIndex . '&submitAdd' . $this->table . '=1&token=' . $this->token . '" method="post" class="width3">
' . ($obj->id ? '<input type="hidden" name="id_' . $this->table . '" value="' . $obj->id . '" />' : '').'
<fieldset><legend><img src="../img/admin/profiles.png" />' . $this->l('Profiles') . '</legend>
<label>'.$this->l('Name:').' </label>
<div class="margin-form">';
foreach ( $languages as $language )
echo '
<div id="name_' . $language['id_lang'|'id_lang'] . '" style="display: ' . ($language['id_lang'|'id_lang'] == $defaultLanguage ? 'block' : 'none') . '; float: left;">
<input size="33" type="text" name="name_' . $language['id_lang'|'id_lang'] . '" value="' . htmlentities( $this->getFieldValue( $obj, 'name', intval( $language['id_lang'|'id_lang'] ) ), ENT_COMPAT, 'UTF-8' ) . '" /><sup>*</sup>
</div>';
$this->displayFlags( $languages, $defaultLanguage, 'name', 'name' );
echo '
<div class="clear"></div>
</div>
<div class="margin-form">
<input type="submit" value="'.$this->l('Save').'" name="submitAdd'.$this->table.'" class="button" />
</div>
<div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
</fieldset>
</form> ';
}
}
?>
|
...