Home is for the experience. Projects are for the ones who are interested!

How To Create A Form Using Helper Form In Prestashop

How To Create A Form Using Helper Form In Prestashop

You can create a form in prestashop using multiple methods and today we will talk about how to create a form using helper form in prestashop. This method is the most advanced because using this method you will be able to add dropdown for multi-language website, I will show in you in the upcoming tutorials how to add multi-language dropdown to a field in prestashop too. Today we will create a form from scratch using helper form.

First of all we have to go in our controller and create the following function:

<?php
public function displayForm(){ 
  $default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); 
  
  $fields_form = array();
  $fields_form[0]['form'] = array(
    'legend' => array(
      'title' => $this->l('My module settings')
    ),
    'input' => array(
      array(
              'type' => 'text',
              'label' => $this->l('Box Description: '),
              'name' => 'box_description'
          )
    ),
    'submit' => array(
      'title' => $this->l('Save'),
          	'class' => 'btn btn-default pull-right'
    )
  );
  $languages = Language::getLanguages();
  $helper = new HelperForm();
  
  
    $helper->name_controller = 'HomepageSettings';
    $helper->token = Tools::getAdminTokenLite('AdminModules');
    
    $helper->default_form_language = $default_lang;
    $helper->allow_employee_form_lang = $default_lang;
  $helper->languages = $this->context->controller->getLanguages();
    
    $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 = 'homepage_settings';
   
  foreach($languages as $k => $v){
    $helper->fields_value['box_description'] = Configuration::get('box_desc');
  }
  return $helper->generateForm($fields_form);
}

Using the above function prestashop will generate for you the structure for your form in tpl file. All you add in $fields_form array will help in creating the main form. Now, to show the form in the template file we will need to create a second function in our create where we will assign a smarty var and we will pass this function as value for the variable. 

The function will look like the following:

public function initContent(){
  parent::initContent();
  
  $this->context->smarty->assign('form', $this->displayForm());
  $content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'your path to tpl file');
  $this->context->smarty->assign(array(
        'content' => $this->content . $content,
    ));
}

As you can see I assigned to the form variable displayForm() function, now the last step is to go in our template file and use the $form var and the form should appear on the page. In addition we can create the postProcess() function where we will handle the update/insert functions for our module. This is one of the methods to create a form using helper form in prestashop, as I said earlier I will make a tutorial for multi-language sites. Let me know if this method worked for you in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *