Aleksandr Sasha
Aleksandr Sasha

Reputation: 168

PrestaShop FormHelper preload multilanguage values

here I am seeing a small problem, I have two tables template and template_text, in template table I store the template type and visibility data and in the template_text table I store the template text in many languages, atm. I have two langs. such as EN and DE. So, when I am editing the template I preload the current template values into the form, but there is a problem with the values like autoemailer_title and autoemailer_body, when the from helper generates the form field it is generating this fields this way autoemailer_title_1, autoemailer_title_2 according the language ID, so, the question would be how to preload all this values the right way? I could use javascript and ajax get request to get the values and then set them by using the input ID, but I think there is a better way using the form helper.

$fields_form = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l($params['title']),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    array(
                        'type' => 'select',                              // This is a <select> tag.
                        'label' => $this->l('Template type'),         // The <label> for this <select> tag.
                        'desc' => $this->l('Choose a template type'),  // A help text, displayed right next to the <select> tag.
                        'name' => 'autoemailer_type',                     // The content of the 'id' attribute of the <select> tag.
                        'required' => true,                              // If set to true, this option must be set.
                        'options' => array(
                            'query' => array(
                                array(
                                    'id_option' => 'static',       // The value of the 'value' attribute of the <option> tag.
                                    'name' => 'Static'   // The value of the text content of the  <option> tag.
                                ),
                                array(
                                    'id_option' => 'dynamic',
                                    'name' => 'Dynamic'
                                ),
                            ),                           // $options contains the data itself.
                            'id' => 'id_option',                           // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
                            'name' => 'name'                               // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
                        )
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('News letter title'),
                        'name' => 'autoemailer_title',
                        'desc' => $this->l('Title'),
                        'lang' => true,
                        // 'style' => 'width:300px',
                        'class' => 'fixed-width-lg',
                    ),
                    array(
                        'type' => 'textarea',
                        'label' => $this->l('Text'),
                        'name' => 'autoemailer_body',
                        'desc' => $this->l('Email body'),
                        'lang' => true,
                        'cols' => 60,
                        'rows' => 10,
                        'class' => 'rte', // we need this for setuping the tiny mce editor
                        'autoload_rte' => true, // we need this for setuping the tiny mce editor
                    ),
                ),
                'submit' => array(
                    'title' => $this->l('Save')
                )
            )
        );

        $helper = new HelperForm();
        $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;
        $this->fields_form = array();

        $helper->identifier = $this->identifier;
        $helper->submit_action = 'newTemplate';
        $helper->currentIndex = $this->getThisUrl() . '&emailer_action=main';
        $helper->token = Tools::getAdminTokenLite('AdminModules');

        return $helper->generateForm(array($fields_form));

Upvotes: 1

Views: 1316

Answers (1)

Aleksandr Sasha
Aleksandr Sasha

Reputation: 168

Solved this one. So, when we have more than one lang then the attribute for which we apply the multilangiage option becomes an array, so to set the value according the language we can just do this way :

$lang_id = 1; // setting the language ID, usually it's done by foreaching the records from ps_lang table

$helper->fields_value['description'][$lang_id] = 'my default value'; // setting the default value

return $helper->generateForm(array($fields_form)); // returning the output of generated form

Upvotes: 1

Related Questions