...
Code Block |
---|
<?php if (!defined('_PS_VERSION_')) { exit; } |
This checks for the existence of an always-existing PrestaShop constant (its version number), and if it does not exist, it stops the module from loading. The sole purpose of this is to prevent malicious visitors to load this file directly.
...
Code Block | ||||
---|---|---|---|---|
| ||||
<?php if (!defined('_PS_VERSION_')) { exit; } class MyModule extends Module { } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
<?php if (!defined('_PS_VERSION_')) { exit; } class MyModule extends Module { public function __construct() { $this->name = 'mymodule'; $this->tab = 'front_office_features'; $this->version = '1.0.0'; $this->author = 'Firstname Lastname'; $this->need_instance = 0; $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('My module'); $this->description = $this->l('Description of my module.'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); if (!Configuration::get('MYMODULE_NAME')) { $this->warning = $this->l('No name provided'); } } } |
Let's examine each line from this first version of the MyModule
class...
...
Code Block |
---|
$this->displayName = $this->l('My module'); $this->description = $this->l('Description of my module.'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); if (!Configuration::get('MYMODULE_NAME')) { $this->warning = $this->l('No name provided.'); } |
These lines respectively assign:
...
Code Block |
---|
public function install() { if (!parent::install()) { return false; return} return true; } |
In this first and extremely simplistic incarnation, this method does the minimum needed: return true returned by the Module
class' install()
method, which returns either true
if the module is correctly installed, or false
otherwise. As it is, if we had not created that method, the superclass' method would have been called instead anyway, making the end result identical.
Nevertheless, we must mention this method, because it will be very useful once we have to perform checks and actions during the module's installation process: creating SQL tables, copying files, creation configuration variables, etc.
...
Code Block |
---|
public function install() { if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_ALL); } if (!parent::install() || !$this->registerHook('leftColumn') || !$this->registerHook('header') || !Configuration::updateValue('MYMODULE_NAME', 'my friend') ) { return false; } return return true; } |
If any of the lines in the testing block fails, the method returns false
and the installation does not happen.
...
Code Block |
---|
public function uninstall() { if (!parent::uninstall()) { return false; } return true; } |
Building on this foundation, we want an uninstall()
method that would delete the data added to the database during the installation ( MYMODULE_NAME
configuration setting). This method would look like this:
Code Block |
---|
public function uninstall() { if (!parent::uninstall() || !Configuration::deleteByName('MYMODULE_NAME') ) { return false; } return true; } |
The Configuration object
...
Code Block |
---|
if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_ALL); } |
As said earlier, here we check that the Multistore feature is enabled, and if so, set the current context to all shops on this installation of PrestaShop.
...