Creating a front-office module
Table of contents
Creating a front-office module
Organizing your module
A module is made of a lot of files, all stored in a folder that bears the same name as the module, that folder being in turn stored in the /modules folder at the root of the main PrestaShop folder.
Default files and folders for a PrestaShop 1.5 module:
"Bootstrap" file:
name_of_the_module.php
Translation files:
fr.php
,en.php
,es.php
, etc., all in the/translations
sub-folderCache configuration file:
config.xml
Module-specific controllers, all in the
/controllers
sub-folderClass-overriding code, all in the
/override
sub-folder (automatic install/uninstall using copy or merge)View files: JavaScript, Models, CSS files, etc.:
/views/css
sub-folder for CSS files/views/js
sub-folder for JavaScript files/views/templates/front
sub-folder for files used by the module controller/views/templates/hooks
sub-folder for files used by the module's hooks
16x16 module logo:
name_of_the_module.jpg
(JPG format)32x32 module logo:
name_of_the_module.png
(PNG format)
The module we are going to create in called "Hello World!". Therefore, its folder is /modules/helloworld
, and its bootstrap file must be /modules/helloworld/helloworld.php
, with a HelloWorld
class defined inside.
Creating the bootstrap file
Here is a very basic first file for the module, named helloworld.php
:
At this stage, the module can only be seen in the "Modules" page in the back-office, in the "Other modules" section.
Adding a constructor
Let's add a first method to our bootstrap code. This method will initialize the module's variables.
$this->name
must be the same as the module's folder name!
$this->tab
is meant to indicate in which module category the module should reside when displayed in the back-office module list. It can take one of the following values:
administration
: Administrationadvertising_marketing
: Advertising & Marketinganalytics_stats
: Analytics & Statsbilling_invoicing
: Billing & Invoicescheckout
: Checkoutcontent_management
: Content Managementexport
: Exportfront_office_features
: Front Office Featuresi18n_localization
: I18n & Localizationmarket_place
: Market Placemerchandizing
: Merchandizingmigration_tools
: Migration Toolsothers
: Other Modulespayments_gateways
: Payments & Gatewayspayment_security
: Payment Securitypricing_promotion
: Pricing & Promotionquick_bulk_update
: Quick / Bulk updatesearch_filter
: Search & Filterseo
: SEOshipping_logistics
: Shipping & Logisticsslideshows
: Slideshowssmart_shopping
: Smart Shoppingsocial_networks
: Social Networks
The install() and uninstall() methods
These two methods make it possible to control what happens when the store administrator installs or uninstalls the module.
In this example, we perform the following tasks during installation:
check that the module is not already installed
check that the module is not added to the
leftColumn
hookcheck that the module is not added to the
header
hookcreate the
HELLO_WORLD_NAME
configuration setting, setting its value to "World"
For its part, the uninstallation method simply deletes the HELLO_WORLD_NAME
configuration setting.
Building the config.xml file
This file makes it possible to optimize the loading of the module list in the back-office.
A few details:
is_configurable
indicates whether the module has a configuration page or not.need_instance
indicates whether an instance of the module must be created when it is displayed in the module list. This can be useful if the module has to perform checks on the PrestaShop configuration, and display warning message accordingly.limited_countries
is used to indicate the countries to which the module is limited. For instance, if the module must be limited to France and Spain, use<limited_countries>fr,es</limited_countries>
.
This module can be created automatically by PrestaShop.
Adding a configuration page
Your module can get a "Configure" link in the back-office module list, and therefore let the user change some settings. This "Configure" link appears with addition of the getContent()
method.
The configuration form itself is displayed with the displayForm()
method:
Implementing hooks
As is, the module does not do much. In order to display something on the front-office, we have to add support for a few hooks. This is done by implementing the hooks' methods:
hookDisplayLeftColumn()
: will hook code into the left column – in our case, it will fetch theHELLO_WORLD_NAME
module setting and display the module's template file,helloworld.tpl
hookDisplayRightColumn()
: will simply do the same ashookDisplayLeftColumn()
hookDisplayHeader()
: will add a link to our CSS file,/views/css/helloworld.css
Template and CSS files
Here is our template file, located in /views/templates/hook/helloworld.tpl
:
...and our CSS file, located in /views/css/helloworld.css
:
Last updated