Jump to content

How to create a complete helper form select, input, radio button, select and option on prestashop 1.7


Mcey

Recommended Posts

 

Hello,

I would like to program a complete form with helperform on prestashop 1.7. with select, radio button and checkbox.

My image attached.
It would be an admin side module.

And all the information will be stocked in an XML file.

I’m in training course and I have 4 weeks to do it.
I followed the tutorial at this address:
http://doc.prestashop.com/display/PS17/Adding+a+configuration+page

So I can just create inputs.

Here is my code:

<?php
if (!defined('_PS_VERSION_')){
  exit;
}

class ModulePresta extends Module
{
  public function __construct()
  {
    $this->name = 'modulepresta';
    $this->tab = 'front_office_features';
    $this->version = '1.0.0';
    $this->author = 'Prenom Nom';
    $this->need_instance = 0;
    $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
    $this->bootstrap = true;

    parent::__construct();

    $this->displayName = $this->l('Mon module presta');
    $this->description = $this->l('Description of my module presta.');

    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');

    if (!Configuration::get('MYMODULE_PRESTA'))
      $this->warning = $this->l('No name provided');
  }


  public function install()
  {
    if (Shop::isFeatureActive())
    {
        Shop::setContext(Shop::CONTEXT_ALL);
    }

    if (!parent::install() ||
        !$this->registerHook('header') ||
        !Configuration::updateValue('MYMODULE_PRESTA', 'Module presta')
       )
      return false;
    return true;
  }

  public function uninstall()
  {
    if (!parent::uninstall() ||
    !Configuration::deleteByName('MYMODULE_PRESTA')
   )
      return false;
    return true;
  }

  public function getContent()
  {
      $output = null;
      if (Tools::isSubmit('submit'.$this->name))
      {
          $my_module_name = strval(Tools::getValue('MYMODULE_PRESTA'));
          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_PRESTA', $my_module_name);
            $output .= $this->displayConfirmation($this->l('Settings updated'));
          }
          Configuration::updateValue('MYMODULE_PRESTA1', Tools::getValue('MYMODULE_PRESTA1'));
      }
      return $output.$this->displayForm();
  }
  
  public function displayForm()
  {
      // Get default language
      $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
      // Init Fields form array
      $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_PRESTA',
                  'size' => 20,
                  'required' => true
              ),
              array(
                'type' => 'text',
                'label' => $this->l('Heure : '),
                'name' => 'MYMODULE_PRESTA1',
                'size' => 20,
                'required' => true
            )              
          ),
          array(
            'type' => 'select',
            'label' => $this->l('Languages:'),
            'name' => 'category',
            'required' => true,
            'options' => array(
                        'query' => $options = array(
                                    array(
                                      'id_option' => 1,       // The value of the 'value' attribute of the <option> tag.
                                      'name' => 'EN',    // The value of the text content of the  <option> tag.
                                    ),
                                    array(
                                      'id_option' => 2,
                                      'name' => 'BG',
                                    ),
                        ),
                        'id' => 'id_option',
                        'name' => 'name',
                       ),
                    ),
         
          'submit' => array(
              'title' => $this->l('Save'),
              'class' => 'btn btn-default pull-right'
          )
      );

      $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_PRESTA'] = Configuration::get('MYMODULE_PRESTA');
      $helper->fields_value['MYMODULE_PRESTA1'] = Configuration::get('MYMODULE_PRESTA1');

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

}

Currently I managed to show that 2 inputs

image.png.12802aab479068cbb497d72215e2876e.png

On the other hand I saw on other forums that it is necessary to use the following methods:
 public function renderForm ()
 public function postProcess ()

while I only use the basic methods:
public function getContent ()
public function displayForm ()

Your help would be precious to me.

prestashop complete form.PNG

Link to comment
Share on other sites

So, I found out how to code the form.

Here is the code if someone is interested by prestashoop 1.7 form :

<?php
if (!defined('_PS_VERSION_')){
  exit;
}

class ModulePresta extends Module{

  public function __construct()
  {
    $this->name = 'modulepresta';
    $this->tab = 'front_office_features';
    $this->version = '1.0.0';
    $this->author = 'Prenom Nom';
    $this->need_instance = 0;
    $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
    $this->bootstrap = true;

    parent::__construct();

    $this->displayName = $this->l('Mon module presta');
    $this->description = $this->l('Description of my module presta.');

    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');

    if (!Configuration::get('MYMODULE_PRESTA'))
      $this->warning = $this->l('No name provided');
  }

  public function install()
  {
    if (Shop::isFeatureActive())
    {
        Shop::setContext(Shop::CONTEXT_ALL);
    }

    if (!parent::install() ||
        !$this->registerHook('header') ||
        !Configuration::updateValue('MYMODULE_PRESTA', 'Module presta')
       )
      return false;
    return true;
  }

  public function uninstall(){
    if (!parent::uninstall() ||
    !Configuration::deleteByName('MYMODULE_PRESTA')
   )
      return false;
    return true;
  }

  public function getContent(){

      $output = null;
      if (Tools::isSubmit('submit'.$this->name))
      {
          $my_module_name = strval(Tools::getValue('MYMODULE_PRESTA'));
          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_PRESTA', $my_module_name);
            $output .= $this->displayConfirmation($this->l('Settings updated'));
          }
          Configuration::updateValue('MYMODULE_PRESTA1', Tools::getValue('MYMODULE_PRESTA1'));
          //Configuration::updateValue('density', Tools::getValue('density'));
      }
      return $output.$this->displayForm();
  }

  
  public function displayForm(){

      // Get default language
      $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

      // Init Fields form array
      $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_PRESTA',
                  'size' => 20,
                  'required' => true
              ),
  
            // Select
            array(
              'type' => 'select',
              'label' => $this->l('Events:'),
              'name' => 'events',
              'required' => true,
              'options' => array(
                'query' => $idevents = array(
                    array(
                        'idevents' => 1,
                        'name' => 'methode 1'
                    ),
                    array(
                        'idevents' => 2,
                        'name' => 'methode 2'
                    ),  
                    array(
                        'idevents' => 3,
                        'name' => 'methode 3'
                    ),                                        
                ),
                'id' => 'idevents',
                'name' => 'name'
              )
            ),

            // Checkbox
            array(
              'type' => 'checkbox',
              'label' => $this->l('Choix'),
              'desc' => $this->l('Faites vos choix.'),
              'name' => 'choix',
              'values' => array(
                  'query' => $lesChoix = array(
                    array(
                        'check_id' => '1',
                        'name' => $this->l('Valeur 1'),
                    ),
                    array(
                        'check_id' => '2',
                        'name' => $this->l('Valeur 2'),
                    ),
                    array(
                        'check_id' => '3',
                        'name' => $this->l('Valeur 3'),
                    )
                ),
                  'id' => 'check_id',
                  'name' => 'name',
                  'desc' => $this->l('Please select')
                ),
            // EN OPTION : On peut cacher les champs ici avec un bouton plus ou moins
            //   'expand' => array(
            //         ['print_total'] => count($option),
            //         'default' => 'show',
            //         'show' => array('text' => $this->l('show'), 'icon' => 'plus-sign-alt'),
            //         'hide' => array('text' => $this->l('hide'), 'icon' => 'minus-sign-alt'),
            //   ),
            ),

            // Radio button
            array(
                'type' => 'radio',
                'label' => $this->l('Density'),
                'name' => 'density',
                'class' => 't',
                'required'  => true,
                'is_bool' => true, 
                'values' => array(
                    array(
                        'id' => 'petit',
                        'value' => 0,
                        'label' => $this->l('petit')
                    ),
                    array(
                        'id' => 'moyen',
                        'value' => 1,
                        'label' => $this->l('moyen')
                    ),
                    array(
                        'id' => 'grand',
                        'value' => 2,
                        'label' => $this->l('grand')
                    )
                ),
            ),

            // Dates
            array(
                'type' => 'date',
                'label' => $this->trans('From', array(), 'Admin.Global'),
                'name' => 'date_from',
                'maxlength' => 10,
                'required' => true,
                'hint' => $this->trans('Format: 2011-12-31 (inclusive).', array(), 'Admin.Orderscustomers.Help')
            ),
            array(
                'type' => 'date',
                'label' => $this->trans('To', array(), 'Admin.Global'),
                'name' => 'date_to',
                'maxlength' => 10,
                'required' => true,
                'hint' => $this->trans('Format: 2012-12-31 (inclusive).', array(), 'Admin.Orderscustomers.Help')
            )
        ),// --- end of input
         
          'submit' => array(
              'title' => $this->l('Save'),
              'class' => 'btn btn-default pull-right'
          )
      );


      $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_PRESTA'] = Configuration::get('MYMODULE_PRESTA');
      $helper->fields_value['events'] = $idevents; // Pour le select
      $helper->fields_value['check_id'] = false;
      $helper->fields_value['density_active_on'] = true;
      $helper->fields_value = array(
        'date_from' => date('Y-m-d'),
        'date_to' => date('Y-m-d')
    );

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


  public function initContent(){
    $this->content .= $this->initFormByDate();
  }

}

 

Now I am looking for how to save these information in an XML file.

Questions remain unanswered regarding the different methods to use in a module.

see you

 

 

  • Thanks 2
Link to comment
Share on other sites

Hi,

In order to put these information in a xml file you just have to add the specific code between these lines :
 

if (Tools::isSubmit('submit'.$this->name))
      {
         //Save your xml values
         //@see http://php.net/manual/en/simplexml.examples-basic.php
      }

Regards,

Link to comment
Share on other sites

  • 1 year later...
  • 4 months later...

@Mcey your listing is way more helpful than docs (which is moved to: https://devdocs.prestashop.com/1.7/modules/creation/adding-configuration-page/)

But, can you please tell me how to check if desired checkbox was selected or not. Or how to set a value to it?

[EDIT]

OK, i know, how to get value, it is simple:

$test = Tools::getValue('cehckbox_name');
// you will get: "on", or false/null

[/EDIT]


I've been trying with: 'value' => 1 in 'query' items array, but have no luck.

[EDIT]So there is no point to try to set value to checkox[/EDIT]

Edited by bumerang07
I've found the solution (see edit history)
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...