简体   繁体   中英

Prestashop 1.7 create admin module

Hi i'm new to prestashop and i try to create an admin module to 1.7. I would create a new menu to display a template and manage my DB.

modules/mymodule/mymodule.php :

<?php
if (!defined('_PS_VERSION_'))
{
    exit;
}

class MyModule extends Module
{
    public function __construct()
    {
        $this->name = 'mymodule';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'John doe';

        $this->bootstrap = true;
        parent::__construct();

        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
        $this->displayName = $this->l('Mon module');
        $this->description = $this->l('On test la creation de module presta.');
    }

    public function install()
    {
         // Install Tabs
        $tab = new Tab();
        $tab->active = 1;
        $tab->class_name = "MyModule";
        $tab->module = 'mymodule';
        $tab->name = array();
        $tab->id_parent = (int)Tab::getIdFromClassName('SELL');
        $tab->position = 3;
        foreach ($lang as $l) {
            $tab->name[$l['id_lang']] = $this->l('Mon module');
        }

        $tab->add();

        if (!parent::install())
            return false;
        return true;
    }

    public function uninstall()
    {
        // Uninstall Tabs
        $tab = new Tab((int)Tab::getIdFromClassName('Mymodule'));
        $tab->delete();

        // Uninstall Module
        if (!parent::uninstall())
            return false;
        return true;
    }
}

module/mymodule/controller/admin/MyModuleController.php :

<?php
class MyModuleController extends ModuleAdminController
{
    public function renderList() {
        $this->content = $this->createTemplate('mymodule.tpl')->fetch();
        return $this->content;
    }
}
?>

modules/mymodule/views/templates/admin/mymodule.tpl:

{block name="page_title"}
    {l s='Mon module'}
{/block}
<section >
    <div>Hello world !</div>
</section>

I create this with a compilation of a lot of tutorials 1.7 / 1.6 but the installation fail. Prestashop provide us a documentation to create a first module but it's not really documented and i find nothing really helpfull on internet yet for 1.7.

Any help/suggestions ?

Thanks

EDIT1 : ok, the install is correct, my tab is created but when i click on it it calls "controller=MyModule" and the module controller is not found. Almost finished.

Module's install() method must return a true/false.

As for template loading from ModuleAdminController

$this->createTemplate($template); 

tries to find template and load the first one it finds in:

  1. /current_theme/modules/yourmodule/views/templates/admin/
  2. /modules/yourmodule/views/templates/admin/

So if you call createTemplate() with a path it's appended to either of the two folders above and it doesn't find your template and that throws out an error.

Move your template into modules/yourmodule/views/templates/admin/ and call create method with only template name.

$this->createTemplate('mymodule.tpl');

It seems that there is something wrong with the install() function of your module. The hook that you are registering seems wrong as well. Try the following code, it worked for us:

public function install()
    {
        if (!parent::install() ||
                !$this->registerHook('header')) {
            return false;
        }
        if (Shop::isFeatureActive()) {
            Shop::setContext(Shop::CONTEXT_ALL);
        }

        $lang = Language::getLanguages();
        $tab = new Tab();
        $tab->class_name = 'AdminTestimonialSetting';
        $tab->module = 'testimonial';
        $tab->id_parent = 2;
        $tab->position = 6;
        foreach ($lang as $l) {
            $tab->name[$l['id_lang']] = $this->l('Testimonial');
        }

        $tab->save();

        return true;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM