How To Create Custom Template For Admin Controller Prestashop

In this tutorial I will show you step-by-step how to implement a module from scratch and create a custom template for admin controller and show it as a tab in the left back office menu. Is quite complicated to implement this functionality in Prestashop if you are begginer because you do not find so many tutorials about methods to implement features from scratch or about best practices. This tutorial is for Prestashop 1.7 but I guess this implementation should work for previews versions of Prestashop too. 

First of all we will go in the root modules folder and we will create a new folder there with any name you want: for this tutorial I will use mymodule name. Then in the mymodule folder we will create 2 more folders: controllers and views.

Your structure should look something like that:

  • mymodule
    • controllers
    • views

Now we need the main file of the module, where we will use the main functions like install(), uninstall() and so on. So, let's create a new file in mymodule folder: mymodule.php. In this file put the following code:

<?php

if (!defined('_PS_VERSION_'))
    exit;

class MyModule extends Module{
  const CLASS_NAME = 'AdminMymodule';

  public function __construct(){ 
    $this->name = 'mymodule';
    $this->version = 1.0;
   
      parent::__construct();
   
      $this->displayName = $this->l('My Module');
      $this->description = $this->l('Description of my module.');

    
    if ($this->active && Configuration::get('my_module') == '')
      $this->warning = $this->l('You have to configure your module');
    
    $this->errors = array();
  }
   
  public function installTab(){
        $tab = new Tab();
        $tab->active = 1;
        $tab->class_name = static::CLASS_NAME;
        $tab->name = array();
        foreach (Language::getLanguages(true) as $lang) {
            $tab->name[$lang['id_lang']] = "My Module";
        }
    $tab->id_parent = (int) Tab::getIdFromClassName('AdminCatalog');
        $tab->module = $this->name;
        return $tab->add();
    }
  
   public function install(){ 
    if (!parent::install())
      return false;
        $this->installTab()
        && $this->registerHook('displayAdminNavBarBeforeEnd')
        && $this->registerHook('displayAdminAfterHeader')
        && $this->registerHook('displayBackOfficeHeader');
    return true;
    	
     }
   public function uninstallTab(){
        $id_tab = (int)Tab::getIdFromClassName(static::CLASS_NAME);
        if ($id_tab) {
            $tab = new Tab($id_tab);
            return $tab->delete();
        } else {
            return false;
        }
    }
   public function uninstall(){
  		if (!parent::uninstall())
    		return false;
    $this->uninstallTab();
  		parent::uninstall();
  	}

  
}

Much of these functions are the default prestashop functions, the main function here is installTab() function because with this one we will create the tab in the left admin menu. There are a few things we have to point out like the next one:

const CLASS_NAME = 'AdminTemplates';

This line will be used in the install Tab function and it return the name of the controller we will use a little later in our implementation, where we will have to assign the template to controller in order to our tab to work.

$tab->id_parent = (int) Tab::getIdFromClassName('AdminCatalog');

At this line, we tell Prestashop where our Tab should be positioned, as you can see we are using here AdminCatalog, that's mean the tab will appear under "Catalog" dropdown from admin left menu. Of course this positioning can be changed any time, you will find on google all names for the dropdowns. getIdFromClassName() function is used to get the ID for this dropdown, if you open your phpmyadmin and search for _PREFIX_tab table you will find where all this data will be stored and you can change from there, or directly from this code.

Now, let's create another folder under controllers folders and name it: admin. In admin folder create a file with the following name: AdminMymoduleController.php 

You should have the following structure:

  • mymodule
    • controllers
      • admin
        • AdminMymoduleController.php
  • views
  • mymodule.php

In the controller file add the following code:

<?php

require_once(_PS_MODULE_DIR_.'/customtemplates/customtemplates.php');

class AdminTemplatesController extends ModuleAdminController  {
   public function __construct()   {
              parent::__construct();
              $this->bootstrap = true;
    }

  public function initContent(){
        parent::initContent();     
    $content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'customtemplates/views/templates/admin/mymodule.tpl');
    $this->context->smarty->assign(array(
             'content' => $this->content . $content,
        ));
    
    }
}

All we need to do know now is to create a new file where we will add content for the page. Create mymodule.tpl file as in the following structure:

  • mymodule
    • controllers
      • admin
        • AdminmymoduleController.php
    • views
      • templates
        • admin
          • mymodule.tpl
    • mymodule.php

This should be the structure for the module. If you follow all steps you should see the Tab in your left admin menu. In the following tutorials I will add new mothods for how to create custom template in prestashop admin.

0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Guille
Guille
4 years ago

I’m working on Prestashop 1.7.6, and try this , Tab is created but when I click over Tab get error:
The server returned a “500 Internal Server Error”.
Do you can help me to resolve this ?

Stifler
2 years ago

thank you. it was helpful.

RuS
RuS
1 year ago

thank’s it’s working for 1.6

3
0
Would love your thoughts, please comment.x
()
x