Child pages
  • Fundamentals

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titlemymodule.php
borderStylesolid
<?php

class MyModule extends Module
  {
  public function __construct()
    {
    $this->name = 'mymodule';
     $this->tab = 'Test';
     $this->version = 1.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;
    }
  }
?>

...

Code Block
$this->name = 'mymodule';

Assigns a 'name' attribute to our class' instance.

Code Block
$this->tab = 'Test';

Assigns a 'tab' attribute to our class' instance. This is the title for the table that shall contain this module in PrestaShop's back-office modules list. You may use an existing name, such as Products, Blocks or Stats, or a custom, as we did here. In this last case, a new table will be created with your title.

Code Block
$this->version = 1.0;

Version number for the module, displayed in the modules list.

...

Code Block
public function install()
  {
  if (parent::install() == false)
    return false;
   return true;
  }

As it is, this method is useless, since all it does is check the value returned by the Module class' install() method. Moreover, if we hadn't 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 uninstall()
  {
  Db::getInstance()->Execute( 'DELETE FROM `' . _DB_PREFIX_ . 'block_cms` WHERE `id_block` = ' . intval($this->id) );
   parent::uninstall();
  }

To put the finishing touch to this basic module, we can add an icon, which will be displayed next to the module's name in the back-office modules list.
The icon file must respect these requirements:

...

Now that all basics are in place, put the module's folder in the /modules folder of your PrestaShop test install, open PrestaShop, and in the "Modules" tab, under "Other Modules", you should find your module. Install it in order to be able to manager it for the rest of this guide.

Hooking a module

Displaying data, starting a process at a specific time: in order for a module to be "attached" to a location on the front-office or the back-office, you need to give it access to one of the above cited hooks.

To that effect, we are going to change your module's code, and add these lines:

Code Block
titlemymodule.php (partial)
borderStylesolid
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);
  }

Let's explore these new or changed lines.

Code Block

if ( parent::install() == false OR !$this->registerHook('leftColumn') )

We changed the original line to add a second test. Now this line reads this way: if installation or hooking fail, we inform PrestaShop.

Code Block

public function hookLeftColumn($params)
  {
  global $smarty;
  return $this->display(__FILE__, 'mymodule.tpl');
  }

The hookLeftColumn() method makes it possible for the module to hook into the theme's left column.
$smarty is the global variable for the Smarty template system, which PrestaShop uses, and which we need to access.
The display() method returns the content of the mymodule.tpl template file, if it exists.

Code Block
public function hookRightColumn($params)
  {
  return $this->hookLeftColumn($params);
  }

Likewise, hookRightColumn() gives access to the theme's right column. In this example, we simply call the hookLeftColumn() method, in order to have the very same display, whatever the column.

Save your file, and already you can hook it into the theme, move it around and transplant it: go to the "Positions" sub-tab for the "Modules" tab in the back-office, then click on the "Transplant a module" link.

In the transplantation form, find "My module" in the modules drop-down menu, then choose "Left column blocks" in the "Hook into" drop-down menu. Save.

Image Added

The "Positions" page should reload, with the following message: "Module transplanted successfully to hook". Congratulations! Scroll down, and you should indeed see your module among the other modules from the "Left column blocks" list.

Displaying content