Jump to content

How display value form config table in field of config page of module?


Trampino

Recommended Posts

I created simple module. Everything works ok, except this:
When I enter value to field in modul config form and press Save, than value is saved to database table ps_configuration. But, if I open modul config form once again, than field is empty and value in database table ps_configuration is empty too. Row in table is not deleted, but it is set to empty and I do not know why.

 

There must be some stupid error in my code. But I do not know where. :-(  Can you help me, pleas?

 

Here is code of my methods

 

    public function getContent()
    {
        /**
         * If values have been submitted in the form, process.
         */
        $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 the form that will be displayed in the configuration of your module.
     */
    protected function renderForm()
    {
        $helper = new HelperForm();

        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $helper->module = $this;
        $helper->default_form_language = $this->context->language->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submitSDFPFModule';
        $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 your inputs */
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
        );

        return $helper->generateForm(array($this->getConfigForm()));
    }

   protected function getConfigForm()
   {
    return array(
      'form' => array(
      'legend' => array(
      'title' => $this->l('Settings'),
      'icon' => 'icon-cogs',
       ),
      'input' => array(
              array(
                        'col' => 3,
            'size' => 3,
            'maxlength' => 3,            
                        'type' => 'text',
                        'prefix' => '<i class="icon icon-envelope"></i>',
                        'desc' => $this->l('Enter seperator'),
                        'name' => 'SDFPF_SEPARATOR',
                        'label' => $this->l('Separator'),
               ),
       ),
      'submit' => array(
      'title' => $this->l('Save'),
      ),
     ),
    );
   }

/**
* Set values for the inputs.
*/
protected function getConfigFormValues()
{
return array(
             'SDFPF_SEPARATOR' => Tools::getValue('SDFPF_SEPARATOR', Configuration::get('SDFPF_SEPARATOR')),

);
}


    /**
     * Save form data.
     */
    protected function _postProcess()
    {
        $form_values = $this->getConfigFormValues();

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

 

Link to comment
Share on other sites

  • 3 months later...

I created simple module. Everything works ok, except this:

When I enter value to field in modul config form and press Save, than value is saved to database table ps_configuration. But, if I open modul config form once again, than field is empty and value in database table ps_configuration is empty too. Row in table is not deleted, but it is set to empty and I do not know why.

 

There must be some stupid error in my code. But I do not know where. :-(  Can you help me, pleas?

 

Here is code of my methods

 

    public function getContent()

    {

        /**

         * If values have been submitted in the form, process.

         */

        $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 the form that will be displayed in the configuration of your module.

     */

    protected function renderForm()

    {

        $helper = new HelperForm();

 

        $helper->show_toolbar = false;

        $helper->table = $this->table;

        $helper->module = $this;

        $helper->default_form_language = $this->context->language->id;

        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

 

        $helper->identifier = $this->identifier;

        $helper->submit_action = 'submitSDFPFModule';

        $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 your inputs */

            'languages' => $this->context->controller->getLanguages(),

            'id_language' => $this->context->language->id,

        );

 

        return $helper->generateForm(array($this->getConfigForm()));

    }

 

   protected function getConfigForm()

   {

    return array(

      'form' => array(

      'legend' => array(

      'title' => $this->l('Settings'),

      'icon' => 'icon-cogs',

       ),

      'input' => array(

              array(

                        'col' => 3,

            'size' => 3,

            'maxlength' => 3,            

                        'type' => 'text',

                        'prefix' => '<i class="icon icon-envelope"></i>',

                        'desc' => $this->l('Enter seperator'),

                        'name' => 'SDFPF_SEPARATOR',

                        'label' => $this->l('Separator'),

               ),

       ),

      'submit' => array(

      'title' => $this->l('Save'),

      ),

     ),

    );

   }

 

/**

* Set values for the inputs.

*/

protected function getConfigFormValues()

{

return array(

             'SDFPF_SEPARATOR' => Tools::getValue('SDFPF_SEPARATOR', Configuration::get('SDFPF_SEPARATOR')),

 

);

}

 

 

    /**

     * Save form data.

     */

    protected function _postProcess()

    {

        $form_values = $this->getConfigFormValues();

 

        foreach (array_keys($form_values) as $key)

            Configuration::updateValue($key, Tools::getValue($key));

    }

 

 

Hi,

This instruction is reposible to set the configuration input value

$helper->fields_value['MYMODULE_INPUT_NAME'] = Configuration::get('MYMODULE_INPUT_NAME');

Hope it helps someone else with same problem.

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