Jump to content

Create a simple multilingual module


Recommended Posts

Hi,

I created a simple module with three values (to display a text, it can be hide or not and it can be on auto-scroll or not, it goes on the displayBanner PS hook). Two are boolean, third one is text. Values are saved in Configuration table, everything works.

I would like to transform this module so multilingual is handled. I looked at different module to try to understand (I'm new to PrestaShop and PHP), I think I can save the booleans in Configuration and create a table for the text (one with just the ID, another one to handle lang), but I'm a bit lost when it gets to save or get the values back.

Any help, hints will be reaaaally appreciated! Dream will be a simple working php file module so I can understand the mechanic well. 🤩


Here is what I've got that I think it's correct (but totally not sure 😅 ) : install, uninstall functions with table create and delete.

public function install()
    {
        if (
            parent::install() &&
            $this->registerHook('header') &&
            $this->registerHook('displayBanner')
        ) {
            $res = true;

            /* Set header banner configuration */
            $res &= Configuration::updateValue('BANNER_SCROLL', false);
            $res &= Configuration::updateValue('BANNER_DISPLAY', false);

            /** Create tables for text to handle multilingue */
            $res &= $this->installDB();

            return $res;
        }

        return false;
    }
 public function uninstall()
    {
        if (parent::uninstall()) {

            $res = true;

            /* Unset configuration */
            $res &= Configuration::deleteByName('BANNER_SCROLL');
            $res &= Configuration::deleteByName('BANNER_DISPLAY');

            /* Delete text tables */
            $res = $this->uninstallDB();

            return $res;
        }

        return false;
    }
 protected function installDB()
    {
        $res = true;

        $res &= Db::getInstance()->execute('
            CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'headerbanner` (
                `id_headerbanner` INT UNSIGNED NOT NULL AUTO_INCREMENT,
                PRIMARY KEY (`id_headerbanner`)
            ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8;'
        );

        $res &= Db::getInstance()->execute('
                CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_. 'headerbanner_lang` (
                `id_headerbanner` INT UNSIGNED NOT NULL,
                `id_lang` INT(10) UNSIGNED NOT NULL ,
                `text` text NOT NULL,
                PRIMARY KEY (`id_headerbanner`, `id_lang`)
            ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;'
        );

        return $res;
    }
protected function uninstallDB()
    {
        return Db::getInstance()->execute('
            DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'headerbanner`, `' . _DB_PREFIX_ . 'headerbanner_lang`;
        ');
    }

Load configuration form

 public function getContent()
    {
        /**
         * If values have been submitted in the form, process.
         */
        if (((bool) Tools::isSubmit('submit_headerbannerModule')) == true) {
            $this->postProcess();
        }

        $this->context->smarty->assign('module_dir', $this->_path);

        $output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');

        return $output . $this->renderForm();
    }

Create form for configuration page

protected function renderForm()
    {
        $helper = new HelperForm();
        $helper->module = $this;
        $helper->show_toolbar = false;
        $helper->table = $this->table;

        $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
        $helper->default_form_language = $lang->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;

        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submit_headerbannerModule';
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
        . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');

        $helper->tpl_vars = array(
            'fields_value' => $this->getConfigFormValues(), /* Add values for inputs */
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
        );
        
        return $helper->generateForm(array($this->getConfigForm()));
    }

Set values for input, here I'm a bit lost

protected function getConfigFormValues()
    {

        $fields = array();
        $languages = Language::getLanguages(false);

        foreach ($languages as $lang) {
            $fields['text'][$lang['id_lang']] = Tools::getValue('text_' . (int) $lang['id_lang']);
        }

        return array(
            'HEADERBANNER_TXT' => $fields,
            'HEADERBANNER_SCROLL' => Configuration::get('HEADERBANNER_SCROLL', false),
            'HEADERBANNER_DISPLAY' => Configuration::get('HEADERBANNER_DISPLAY', true)
        );
    }

Save the data, this is the original code without multilingual handling, I don't know what to do here to save booleans in configuration table but text in its own table...

protected function postProcess()
    {
        $form_values = $this->getConfigFormValues();

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }

Same for the hook, this is the original code

public function hookDisplayBanner()
    {
        if (Configuration::get('HEADERBANNER_DISPLAY')) {
            $this->context->smarty->assign([
                'HEADERBANNER_TXT' => Configuration::get('HEADERBANNER_TXT'),
                'HEADERBANNER_SCROLL' => Configuration::get('HEADERBANNER_SCROLL'),
            ]);

            return $this->display(__FILE__, 'headerbanner.tpl');
        }
    }

 

Link to comment
Share on other sites

Hi @JBW,

Thanks a lot for your answer. I reckon creating a table it's what I do in installDB() (see first post) but I didn't create a class so that's one thing I missed.

I did have a look at ps_customtext but I didn't want to handle multi-shop and I'm not sure if I need the widget functions. 🤔

And where I've got trouble to understand what to do is having text saved in its own table and the two booleans saved somewhere else. I reckon I should try again maybe using the ps_customtext as a base... 😅

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...