Jump to content

Prestashop 1.6 custom module how to add custom input and table in Configuration at Back Office


aljon ngo

Recommended Posts

I am trying to create a custom footer module for my site, It is my first time to create a module in prestashop 1.6 and i read about this article

http://doc.prestashop.com/display/PS16/Adding+a+configuration+page

my problem is i cannot fully understand if i need to create a custom table for my module or not

Configuration::updateValue('FOOTERCUSTOM_NAME', $footercustom);

as you can see in this code, the Configuration table is already created and Configuration::updateValue is a default prestashop code

if i wanted to create a input 1 , input 2 and input 3 at our configuration page in module backoffice and it automatically output the data in the database at my hookfooter. what is the proper way of doing this.

note that i already succeeded in craeting a basic module, my main problem in how to insert data on my database using the configuration in my module

 

Link to comment
Share on other sites

hello

Configuration::updateValue('FOOTERCUSTOM_NAME', $footercustom);

its not a code to create "table",

with this code you create new field in ps_configuration table

 

field name will be "FOOTERCUSTOM_NAME"

 

so if you want several inputs, use:

 

Configuration::updateValue('FOOTERCUSTOM_NAME1', $footercustom);

Configuration::updateValue('FOOTERCUSTOM_NAME2', $footercustom);

Configuration::updateValue('FOOTERCUSTOM_NAME3', $footercustom);

Configuration::updateValue('FOOTERCUSTOM_NAME4', $footercustom);

 

etc.

so, create fields:

<input type="text" name="FOOTERCUSTOM_NAME1" value="'.Configuration::get('FOOTERCUSTOM_NAME1').'"/>
<input type="text" name="FOOTERCUSTOM_NAME2" value="'.Configuration::get('FOOTERCUSTOM_NAME2').'"/>
<input type="text" name="FOOTERCUSTOM_NAME3" value="'.Configuration::get('FOOTERCUSTOM_NAME3').'"/>
<input type="text" name="FOOTERCUSTOM_NAME4" value="'.Configuration::get('FOOTERCUSTOM_NAME4').'"/>

and to update these fields (after submit) use:

Configuration::updateValue('FOOTERCUSTOM_NAME1',Tools::getValue('FOOTERCUSTOM_NAME1'));
Configuration::updateValue('FOOTERCUSTOM_NAME2',Tools::getValue('FOOTERCUSTOM_NAME2'));
Configuration::updateValue('FOOTERCUSTOM_NAME3',Tools::getValue('FOOTERCUSTOM_NAME3'));
Configuration::updateValue('FOOTERCUSTOM_NAME4',Tools::getValue('FOOTERCUSTOM_NAME4'));
  • Like 1
Link to comment
Share on other sites

post-792013-0-05818600-1401932808_thumb.jpg

 

how can i ahchieve this using the

public function getContent()
{
    $output = null;
    if (Tools::isSubmit('submit'.$this->name))
    {
        $my_module_name = strval(Tools::getValue('MYMODULE_NAME'));
        if (!$my_module_name
          || empty($my_module_name)
          || !Validate::isGenericName($my_module_name))
            $output .= $this->displayError($this->l('Invalid Configuration value'));
        else
        {
            Configuration::updateValue('MYMODULE_NAME', $my_module_name);
            $output .= $this->displayConfirmation($this->l('Settings updated'));
        }
    }
    return $output.$this->displayForm();
}
public function displayForm()
{
    // Get default language
    $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
    
    // Init Fields form array
    $fields_form[0]['form'] = array(
        'legend' => array(
            'title' => $this->l('Settings'),
        ),
        'input' => array(
            array(
                'type' => 'text',
                'label' => $this->l('Configuration value'),
                'name' => 'MYMODULE_NAME',
                'size' => 20,
                'required' => true
            )
        ),
        'submit' => array(
            'title' => $this->l('Save'),
            'class' => 'button'
        )
    );
    
    $helper = new HelperForm();
    
    // Module, token and currentIndex
    $helper->module = $this;
    $helper->name_controller = $this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
    $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
    
    // Language
    $helper->default_form_language = $default_lang;
    $helper->allow_employee_form_lang = $default_lang;
    
    // Title and toolbar
    $helper->title = $this->displayName;
    $helper->show_toolbar = true;        // false -> remove toolbar
    $helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
    $helper->submit_action = 'submit'.$this->name;
    $helper->toolbar_btn = array(
        'save' =>
        array(
            'desc' => $this->l('Save'),
            'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
            '&token='.Tools::getAdminTokenLite('AdminModules'),
        ),
        'back' => array(
            'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'desc' => $this->l('Back to list')
        )
    );
    
    // Load current value
    $helper->fields_value['MYMODULE_NAME'] = Configuration::get('MYMODULE_NAME');
    
    return $helper->generateForm($fields_form);
}


Edited by aljon ngo (see edit history)
Link to comment
Share on other sites

I already found the solution,

 

in the public function getContent()

 

i just add 

    Configuration::updateValue('FOOTERCUSTOM_NAME1', Tools::getValue('FOOTERCUSTOM_NAME1'));  

inside the     if (Tools::isSubmit('submit'.$this->name)) statement

 

and this one to show a multiple input

public function displayForm()
	{
		// Get default language
		$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
		 
		// Init Fields form array
		$fields_form[0]['form'] = array(
			'legend' => array(
				'title' => $this->l('Settings'),
			),
			'input' => array(
				array(
					'type' => 'text',
					'label' => $this->l('Configuration value'),
					'name' => 'FOOTERCUSTOM_NAME',
					'size' => 20,
					'required' => true
				),
				array(
					'type' => 'text',
					'label' => $this->l('Link1'),
					'name' => 'FOOTERCUSTOM_NAME1',
				)
			),
			'submit' => array(
				'title' => $this->l('Save'),
				'class' => 'button'
			)
		);		
  • Like 1
Link to comment
Share on other sites

How can i use inputs like arrays?

 

For instance:

'input' => array(
                    array(
                        'type' => 'text',
                        'label' => $this->l('Configuration value'),
                        'name' => 'MYMODULE_NAME[]',
                        'size' => 20,
                        'required' => true
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Configuration value2'),
                        'name' => 'MYMODULE_NAME[]',
                        'size' => 20,
                        'required' => true
                    )
                ),

Link to comment
Share on other sites

Can you explain your question further? what is your goal

 

 

How can i use inputs like arrays?

 

For instance:

'input' => array(
                    array(
                        'type' => 'text',
                        'label' => $this->l('Configuration value'),
                        'name' => 'MYMODULE_NAME[]',
                        'size' => 20,
                        'required' => true
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Configuration value2'),
                        'name' => 'MYMODULE_NAME[]',
                        'size' => 20,
                        'required' => true
                    )
                ),

 

Link to comment
Share on other sites

  • 4 months later...

 

I already found the solution,

 

in the public function getContent()

 

i just add 

    Configuration::updateValue('FOOTERCUSTOM_NAME1', Tools::getValue('FOOTERCUSTOM_NAME1'));  

inside the     if (Tools::isSubmit('submit'.$this->name)) statement

 

and this one to show a multiple input

public function displayForm()
	{
		// Get default language
		$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
		 
		// Init Fields form array
		$fields_form[0]['form'] = array(
			'legend' => array(
				'title' => $this->l('Settings'),
			),
			'input' => array(
				array(
					'type' => 'text',
					'label' => $this->l('Configuration value'),
					'name' => 'FOOTERCUSTOM_NAME',
					'size' => 20,
					'required' => true
				),
				array(
					'type' => 'text',
					'label' => $this->l('Link1'),
					'name' => 'FOOTERCUSTOM_NAME1',
				)
			),
			'submit' => array(
				'title' => $this->l('Save'),
				'class' => 'button'
			)
		);		

You forgot to put these

// Load current value
    $helper->fields_value['FOOTERCUSTOM_NAME1'] = Configuration::get('FOOTERCUSTOM_NAME1');

to see the changes. but Thanks

Link to comment
Share on other sites

  • 8 months later...

I followed the thread and created a new variable:

  public function getContent()
  {
    $output = null;
 
    if (Tools::isSubmit('submit'.$this->name))
    {
      Configuration::updateValue('GEOTHEME_KEY', Tools::getValue('GEOTHEME_KEY'));
      $output .= $this->displayConfirmation($this->l('Settings updated'));
    }
    return $output.$this->displayForm();
  }

  public function displayForm()
  {
      // Get default language
      $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
       
      // Init Fields form array
      $fields_form[0]['form'] = array(
          'legend' => array(
              'title' => $this->l('Settings'),
          ),
          'input' => array(
              array(
                  'type' => 'text',
                  'label' => $this->l('WebService Key'),
                  'name' => 'GEOTHEME_KEY',
                  'size' => 20,
                  'required' => true
              )
          ),
          'submit' => array(
              'title' => $this->l('Save'),
              'class' => 'button'
          )
      );
  /* more helper code */
  }

But I keep getting the error:

Notice on line 387 in file C:\xampp\htdocs\geoPresta\tools\smarty\sysplugins\smarty_internal_templatebase.php(157) : eval()'d code
[8] Undefined index: GEOTHEME_KEY 

My value is also not being saved. What am I doing wrong?

Link to comment
Share on other sites

  • 4 months later...

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