How To Make an Override In Your Module

How To Make an Override In Your Module

Prestashop 1.4 has pleased developers with a possibility to change standard functions behavior without core modification. New Prestashop 1.5 went even further by presenting the possibility to rewrite files while installing a module.

Here is what the install function looked like in Prestashop 1.4:

 

I.e., the system checked the module table for records about the module installed and if there were no any, it made one and returned its ID. In new Prestashop 1.5 this function looks like below:

 

New Prestashop version provides a possibility to specify the system versions your module is compatible with. $this->ps_versions_compliancy[‘min’] and $this->ps_versions_compliancy[‘max’] are used for it; if client’s system version is not compatible with your module, the following message will be displayed for a user: The version of your module is not compliant with your PrestaShop version. installOverrides() и addOverride() functions are responsible for controllers and classes redefining while module installation.

 

In order to make an override in the module, one should create the “override” folder in it and place the file with logic changed. Folder structure should be saved. For instance, to overload CmsController.php, you need to the following folders structure override/controllers/front/CmsController.php with the following content in the folder

 

When your module is installed, the system will find the file it needs to rewrite. Things are not that neat and tidy though. Prestashop will not simply replace the override/controllers/front/CmsController.php file with the one located in the same folder with your module. What if the main file contains already some kind of a changed logic? Protection mechanism launches here: the system compares its main file with overrides (ex. public_html/override/controllers/front/CmsController.php) and file in you module. If functions names are not the same in these files, there will be an installation failure and a user will see, for instance, the following message: The method setMedia in the class CmsController is already overridden. 

I would like to warn you about the harm of overrides after all. They stir up troubles for developers: if user’s store is strongly customized and same class (or function, what is worse) is overridden by several modules at once, logic should be combined into a single file. I.e., if Prestashop displays “this function is already overridden” error, there will be nothing to do except for editing the main file with the rewrite. So, if you are able to use Hook in your module – do it and you’ll avoid the rewrite.



4 comments

  1. Great tutorial about how to add an override in a module. Any word un how to remove the override when you uninstall the module?

  2. Kasper,

    It will be removed itself. When the module is being deleted, overridden methods will be deleted as well. (see Module::uninstallOverrides())

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *