Jump to content

Recommended Posts

Dear prestashop developers,

A few days ago i tryed making my first prestashop module, and to be honest it's kind of a pain in the ass. but its a new challenge so i like it.

but there is a problem is can not get my head arround. and that is the setTemplate() function.

here is my code for my controller of the module :

<?php

class WorkplaceAllController extends ModuleAdminController
{
    public function __construct() {
    	
        parent::__construct();
    }

	public function init()
	{
		parent::init();
	}

	public function initContent()
	{
		$this->setTemplate('all.tpl');
		return parent::initContent();
		
		
	}

}

it isn't much, but i can not load the template 'all' it gets the template from : ../admin646hq9/themes/default/template/

i would like it to get it from : ../module/My_module_name/views/templates/admin/ 

 

when i read the doc's online and watch tutorials this is the directory they use withoud a problem. how can i change it to the directory i want? becouse in this way i can not install the template with the module. 

idk if it is needed but here is my module : 

<?php

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

class workplace extends Module
{



    public function __construct()
    {
        $this->name = 'workplace';
        $this->version = '1.0';
        $this->author = 'Gavin';
        $this->tab = 'administration';
        //$this->controllers = ['all'];

        parent::__construct();

        $this->displayName = 'Workplace Lite';
        $this->description = $this->l('Easy to use workplace manager');
        $this->ps_versions_compliancy = [
            'min' => '1.7.2.0',
            'max' => _PS_VERSION_
        ];
 
        $this->confirmUninstall = $this->l('Are you sure you want to uninstall? Uninstalling this module will result in the lost of saved information in this module! the whole database needed for this module will be removed. if you ever want to use the module again please consider deactivating it!');
 
        if (!Configuration::get('workplace')) {
            $this->warning = $this->l('No name provided');
        }
   
    }

    public function install()
    {
        

    return parent::install()  && $this->createTABLE() && $this->installModuleTab('WorkPlaceLite', array(1=>'Workplace Lite'), 0);
    }

    public function uninstall()
    {
        $this->dropTABLE();
        $this->uninstallModuleTab('WorkPlaceLite');

        if (!parent::uninstall()) {
            return false;
        }
    return true;
    }

    public function dropTABLE()
    {
        $sql= "DROP TABLE `"._DB_PREFIX_."module_workplace`";

        if(!$result=Db::getInstance()->Execute($sql))
        {
            return false;
        }

        return true;
    }

    public function createTABLE()
    {
        $sql= "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."module_workplace`(
            `id_tablename` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
            `title` VARCHAR(256) NOT NULL )";

        if(!$result=Db::getInstance()->Execute($sql))
        {
            return false;
        }

        return true;
    }


    private function installModuleTab($tabClass, $tabName, $idTabParent)
    {    
         // Install Tabs
        $parent_tab = new Tab();
        // Need a foreach for the language
        $parent_tab->name[$this->context->language->id] = $this->l('WorkPlace Lite');
        $parent_tab->class_name = 'AdminWorkPlace';
        $parent_tab->id_parent = 0; // Home tab
        $parent_tab->module = $this->name;
        $parent_tab->add();



        $tabs = [['Workplace','WorkplaceAll'],['New Workplace','WorkplaceNew',]];

        foreach ($tabs as &$value) {
            $tab = new Tab();
            $tab->name[$this->context->language->id] = $this->l($value[0]);
            $tab->class_name = $value[1];
            $tab->id_parent = $parent_tab->id;
            $tab->module = $this->name;
            $tab->add();
        }
        

        return true;
    }

    private function uninstallModuleTab($tabClass)
    {       
        $moduleTabs = Tab::getCollectionFromModule($this->name);
        if (!empty($moduleTabs)) {
            foreach ($moduleTabs as $moduleTab) {
                $moduleTab->delete();
            }
        }

    }

}

 

greating,

Gavin

Link to comment
Share on other sites

22 hours ago, Gavin said:

Dear prestashop developers,

A few days ago i tryed making my first prestashop module, and to be honest it's kind of a pain in the ass. but its a new challenge so i like it.

but there is a problem is can not get my head arround. and that is the setTemplate() function.

here is my code for my controller of the module :


<?php

class WorkplaceAllController extends ModuleAdminController
{
    public function __construct() {
    	
        parent::__construct();
    }

	public function init()
	{
		parent::init();
	}

	public function initContent()
	{
		$this->setTemplate('all.tpl');
		return parent::initContent();
		
		
	}

}

it isn't much, but i can not load the template 'all' it gets the template from : ../admin646hq9/themes/default/template/

i would like it to get it from : ../module/My_module_name/views/templates/admin/ 

 

when i read the doc's online and watch tutorials this is the directory they use withoud a problem. how can i change it to the directory i want? becouse in this way i can not install the template with the module. 

idk if it is needed but here is my module : 


<?php

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

class workplace extends Module
{



    public function __construct()
    {
        $this->name = 'workplace';
        $this->version = '1.0';
        $this->author = 'Gavin';
        $this->tab = 'administration';
        //$this->controllers = ['all'];

        parent::__construct();

        $this->displayName = 'Workplace Lite';
        $this->description = $this->l('Easy to use workplace manager');
        $this->ps_versions_compliancy = [
            'min' => '1.7.2.0',
            'max' => _PS_VERSION_
        ];
 
        $this->confirmUninstall = $this->l('Are you sure you want to uninstall? Uninstalling this module will result in the lost of saved information in this module! the whole database needed for this module will be removed. if you ever want to use the module again please consider deactivating it!');
 
        if (!Configuration::get('workplace')) {
            $this->warning = $this->l('No name provided');
        }
   
    }

    public function install()
    {
        

    return parent::install()  && $this->createTABLE() && $this->installModuleTab('WorkPlaceLite', array(1=>'Workplace Lite'), 0);
    }

    public function uninstall()
    {
        $this->dropTABLE();
        $this->uninstallModuleTab('WorkPlaceLite');

        if (!parent::uninstall()) {
            return false;
        }
    return true;
    }

    public function dropTABLE()
    {
        $sql= "DROP TABLE `"._DB_PREFIX_."module_workplace`";

        if(!$result=Db::getInstance()->Execute($sql))
        {
            return false;
        }

        return true;
    }

    public function createTABLE()
    {
        $sql= "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."module_workplace`(
            `id_tablename` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
            `title` VARCHAR(256) NOT NULL )";

        if(!$result=Db::getInstance()->Execute($sql))
        {
            return false;
        }

        return true;
    }


    private function installModuleTab($tabClass, $tabName, $idTabParent)
    {    
         // Install Tabs
        $parent_tab = new Tab();
        // Need a foreach for the language
        $parent_tab->name[$this->context->language->id] = $this->l('WorkPlace Lite');
        $parent_tab->class_name = 'AdminWorkPlace';
        $parent_tab->id_parent = 0; // Home tab
        $parent_tab->module = $this->name;
        $parent_tab->add();



        $tabs = [['Workplace','WorkplaceAll'],['New Workplace','WorkplaceNew',]];

        foreach ($tabs as &$value) {
            $tab = new Tab();
            $tab->name[$this->context->language->id] = $this->l($value[0]);
            $tab->class_name = $value[1];
            $tab->id_parent = $parent_tab->id;
            $tab->module = $this->name;
            $tab->add();
        }
        

        return true;
    }

    private function uninstallModuleTab($tabClass)
    {       
        $moduleTabs = Tab::getCollectionFromModule($this->name);
        if (!empty($moduleTabs)) {
            foreach ($moduleTabs as $moduleTab) {
                $moduleTab->delete();
            }
        }

    }

}

 

greating,

Gavin

 

 

Try using this code:

 

<?php

class WorkplaceAllController extends ModuleAdminController
{
    	public function init()
	{
		parent::init();
	}

	public function initContent()
	{
		$module_name = 'workplace';

		parent::initContent();

		if(version_compare(_PS_VERSION_, '1.7', '>')) {
            $this->setTemplate('module:' . $module_name . '/views/templates/front/all17.tpl');
        }else {
            $this->setTemplate('all.tpl');
        }
	}

}

 

For Prestashop 1.6.x.x: template all.tpl located here: /modules/workplace/views/templates/front/all.tpl

For Prestashop 1.7.x.x: template all.tpl located here: /modules/workplace/views/templates/front/all17.tpl

 

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...