Jump to content

Création de module - Champs multiples


Recommended Posts

Bonjour à tous,

 

J'ai suivi le tutoriel de la doc de Prestashop 1.6 pour créer un module.

Celui-ci contient un seul champs, et fonctionne très bien.

 

J'ai commencé fork celui-ci pour la création d'un second module qui, lui, devra contenir plusieurs champs (upload d'image + texte) et c'est là que ça bloque. Je ne suis pas un expert PHP, et malgré la doc (& google) je n'ai pas trouvé de solution claire à mon soucis.

 

Je ne sais en fait pas comment déclarer plus d'un champs (notamment pour les getValue & updateValue).

 

Le code actuel ci-dessous (avec un seul champs, dès que j'essaie d'en déclarer un deuxième, je me perds assez vite entre les arrays, les getMultiple etc...) :

<?php
if (!defined('_PS_VERSION_'))
  exit;
 
class ContactTopPage extends Module
{
  public function __construct()
  {
    $this->name = 'contacttoppage';
    $this->tab = 'front_office_features';
    $this->version = '1.0.0';
    $this->author = 'Lorem IPSUM';
    $this->need_instance = 0;
    $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); 
    $this->bootstrap = true;
 
    parent::__construct();
 
    $this->displayName = $this->l('Lorem IPSUM - Contact top page');
    $this->description = $this->l('Édition de la phrase de contact en haut de page.');
 
    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
 
    if (!Configuration::get('CONTACTTOPPAGE'))      
      $this->warning = $this->l('No name provided');
  }

  public function install() {
      parent::install();
      if(!$this->registerHook('Nav')) return false;
      return true;
  }

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

  public function getContent()
  {
      $output = null;
   
      if (Tools::isSubmit('submit'.$this->name))
      {
          $contact_top_page = strval(Tools::getValue('CTP_LIEN'));
          if (!$contact_top_page
            || empty($contact_top_page)
            || !Validate::isGenericName($contact_top_page))
              $output .= $this->displayError($this->l('Invalid Configuration value'));
          else
          {
              Configuration::updateValue('CTP_LIEN', $contact_top_page);
              $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('Contact top page'),
              'icon'  => $this->l('icon-envelope')
          ),
          'input' => array(
              array(
                  'type' => 'text',
                  'label' => $this->l('Lien du formulaire ou de la page de contact'),
                  'name' => 'CTP_LIEN',
                  '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['CTP_LIEN'] = Configuration::get('CTP_LIEN');
       
      return $helper->generateForm($fields_form);
  }


  public function hookDisplayNav($params)
  {
    $this->context->smarty->assign(
        array(
            'contact_top_page' => Configuration::get('CTP_LIEN'),
            'ctp_link' => $this->context->link->getModuleLink('contacttoppage', 'display')
        )
    );
    return $this->display(__FILE__, 'contacttoppage.tpl');
  }
   
                                                                            
}

Il me faudrait arriver à "simplement" ajouter à CTP_LIEN un CTP_TEXTE par exemple.

Du côté du displayForm pas de soucis pour rajouter un ou des inputs supplémentaires, mais je bloque sur le reste.

 

Si vous avez des pistes, je suis complètement preneur !

 

En vous remerciant & vous souhaitant une bonne journée :)

 

Cordialement

Elia PEREZ.

Link to comment
Share on other sites

  • 3 weeks 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...