Fruitcake_Gary
Fruitcake_Gary

Reputation: 21

Prestashop 8.1 create simple module, no redirection to the site

I have a problem in preparing a simple module that displays the word TEST both in the admin panel and on the user's side after accessing his account

Module structure: [1]: https://i.sstatic.net/XZeHl4cg.png

controllers/admin/AdminTestController.php

<?php

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

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

controllers/front/TestController.php

<?php

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

class TestModuleTestModuleTestController extends ModuleFrontController
{
    public function initContent()
    {
        parent::initContent();

        $this->context->smarty->assign([
            'message' => 'TEST'
        ]);

        $this->setTemplate('module:testmodule/views/templates/front/test.tpl');
    }
}

views/template/admin/test.tpl

<h1>TEST</h1>

views/template/front/test.tpl

<!-- modules/testmodule/views/templates/front/test.tpl -->
<div>
    <a href="{$link->getModuleLink('testmodule', 'test')}">SEE DATA</a>
</div>

/testmodule.php

<?php

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

class TestModule extends Module
{
    public function __construct()
    {
        $this->name = 'testmodule';
        $this->tab = 'front_office_features';
        $this->version = '1.0.0';
        $this->author = 'Your Name';
        $this->need_instance = 0;
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Test Module');
        $this->description = $this->l('A simple module to display TEST on both admin and user pages.');

        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
    }

    public function install()
    {
        return parent::install() &&
            $this->registerHook('displayCustomerAccount') &&
            $this->registerHook('displayAdminProductsMainStepLeftColumnMiddle');
    }

    public function hookDisplayCustomerAccount($params)
    {
        $link = $this->context->link->getModuleLink('testmodule', 'test');
        return '<a href="' . $link . '">SEE DATA</a>';
    }
}

The problem is that the link from /front/test.tpl redirects to localhost:8080/en/module/testmodule/test.

I'll just add that I've been using the Prestashop documentation for the most part and yet these errors appear

Another thing is that on the admin side there is no redirect to this view, on other attempts there was an error status 500 and a store operation error, and the link itself led to the same as above only that for admin

The module itself installed without problems, it appears in the /modules tab

Have any of you encountered something like this?

Environment: Docker - windows 10
Prestashop: 8.1

Upvotes: 0

Views: 71

Answers (1)

Paul Albers
Paul Albers

Reputation: 186

From what I see you created the module in the wrong folder. To create a module, you must place this in the Module folder eg modules\test. There you create a file test.php with constructor. Below you see an example how to do it. Replace the displayHookLocation and displayAdminLocation by the proper hook or create your own hook. Here is a list of all hooks in Prestashop: Prestashop hooks

class Test extends Module
{
    public function __construct()
    {
        $this->name = 'Test';
        $this->tab = 'front_office_features';
        $this->version = '1.0.0';
        $this->author = 'Your name';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = [
            'min' => '1.7.0.0',
            'max' => '8.99.99'
        ];
        $this->bootstrap = true;

        parent::__construct();
        
        $this->displayName = $this->trans('Test module', [], 'Modules.Test.Test');
        $this->description = $this->trans('This is a test module', [], 'Modules.Test.Test');
        
        $this->confirmUninstall = $this->trans('Are you sure you want to uninstall?', [], 'Modules.Test.Test');

        if (!Configuration::get('TEST')) {
            $this->warning = $this->trans('No name provided', [], 'Modules.Test.Test');
        }
    }
    
    public function isUsingNewTranslationSystem()
    {
        return true;
    }

    public function getContent()
    {
        $output = '';
        if(Tools::isSubmit('submit' . $this->name))
        {
            $configValue = (string) Tools::getValue('TEST_CONFIG');
            if(empty($configValue) || !Validate::isGenericName($configValue))
            {
                $output = $this->displayError($this->trans('Invalid configuration value', [], 'Modules.Test.Test'));
            }
            else
            {
                Configuration::updateValue('TEST_CONFIG', $configValue);
                $output = $this->displayConfirmation($this->trans('Settings updated', [], 'Modules.Test.Test'));
            }
        }
        return $output;
    }
    
    public function install() {
        return (parent::install() 
                && $this->registerHook('displayHookLocation') 
                && $this->registerHook('displayAdminLocation'));
    }
    
    public function uninstall()
    {
        return (
                parent::uninstall()
                && Configuration::deleteByName('TEST')
        );
    }
    
    public function hookDisplayHookLocation($params)
    {        
        $this->context->smarty->assign(
                [
                    'description_test' => $this->trans('Description test', [], 'Modules.Test.Test')
                ]
        );
        return $this->display(__FILE__, 'views/templates/front/test.tpl');
    }
    
    public function hookDisplayAdminLocation($params)
    {
        $this->context->smarty->assign(
          [
              'description_test' => $this->trans('Test', [], 'Modules.Test.Test')
          ]      
        );
        return $this->display(__FILE__, 'views/templates/admin/test.tpl');
    }
}

Upvotes: 0

Related Questions