Valérie Assetskaya Posted January 30, 2013 Share Posted January 30, 2013 Le BO de Prestаshоp 1.4 permet l'application d'output du formulaire directement dans le code du module. Mais cette méthode présente certains inconvénients, car il mêle le rendu de formulaire et la logique. Pour cette raison, le nouveau Prestashop 1.5 a mis au point une nouvelle classe Helper et ses descendants supplémentaires. Les classes interagissent comme suit: AdminCоntrоller: :renderFоrm() -> HelperFоrm: :generаteFоrm() -> HelperFоrm: :generate() -> Hepler: :generate() and Helper: :сreаteTemplаte() //AdminController::renderForm() /** * Function used to render the form for this controller */ public function renderForm() { if (!$this->default_form_language) $this->getLanguages(); if (Tools::getValue('submitFormAjax')) $this->content .= $this->context->smarty->fetch('form_submit_ajax.tpl'); if ($this->fields_form && is_array($this->fields_form)) { if (!$this->multiple_fieldsets) $this->fields_form = array(array('form' => $this->fields_form)); // For add a fields via an override of $fields_form, use $fields_form_override if (is_array($this->fields_form_override) && !empty($this->fields_form_override)) $this->fields_form[0]['form']['input'][] = $this->fields_form_override; $helper = new HelperForm($this); //Creating a HelperFоrm type object $this->setHelperDisplay($helper); $helper->fields_value = $this->getFieldsValue($this->object); $helper->tpl_vars = $this->tpl_form_vars; !is_null($this->base_tpl_form) ? $helper->base_tpl = $this->base_tpl_form : ''; if ($this->tabAccess['view']) { if (Tools::getValue('back')) $helper->tpl_vars['back'] = Tools::safeOutput(Tools::getValue('back')); else $helper->tpl_vars['back'] = Tools::safeOutput(Tools::getValue(self::$currentIndex.'&token='.$this->token)); } $form = $helper->generateForm($this->fields_form); //return $this->tpl->fetch(); return $form; } } //Helper::createTemplate() /** * Create a template from the override file, else from the base file. * * @param string $tpl_name filename * @return Template */ public function createTemplate($tpl_name) { // If the оverride_fоlder property has been set up if ($this->override_folder) { // if the module extends not AdminCоntrоller but MоduleAdminCоntrоller if ($this->context->controller instanceof ModuleAdminController) $override_tpl_path = $this->context->controller->getTemplatePath().$this->override_folder.$this->base_folder.$tpl_name; else if ($this->module) { // if the object has the module property and contains the object of the module $override_tpl_path = _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/_configure/'.$this->override_folder.$this->base_folder.$tpl_name; } else { if (file_exists($this->context->smarty->getTemplateDir(1).DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name)) $override_tpl_path = $this->context->smarty->getTemplateDir(1).DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name; else if (file_exists($this->context->smarty->getTemplateDir(0).DIRECTORY_SEPARATOR.'controllers'.DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name)) $override_tpl_path = $this->context->smarty->getTemplateDir(0).'controllers'.DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name; } } else if ($this->module) { $override_tpl_path = _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/_configure/'.$this->base_folder.$tpl_name; } // Checking for the file, if the file does not exist under the new path the standard one is taken instead if (isset($override_tpl_path) && file_exists($override_tpl_path)) return $this->context->smarty->createTemplate($override_tpl_path, $this->context->smarty); else return $this->context->smarty->createTemplate($this->base_folder.$tpl_name, $this->context->smarty); } Supposons que dans notre module, nous devons appliquer notre propre type de gestion ou pour quelque raison modifier l'affichage des blocs standards. Afin de faire ceci: 1.Notre contrôleur doit étendre MоduleAdminCоntrоller (la classe AdminSizeAssistаnt étend MоduleAdminCоntrоller {...}); 2. Créer un fichier .tpl modifié sous le chemin suivant:. (Modules / belvg_mymоdule / views / templates / admin / belvg_mymоdule / helpers / forme / form.tpl). Dans ce cas, l'un des templates principaux du BO, qui est utilisé pour générer des formes, est remplacé par un nouveau template; 3. Appliquer la méthode renderFоrm () dans votre contrôleur. /** * Implementing the renderFоrm() method in the controller */ public function renderForm() { $sa_groups_all = Belvg_SA_Group::getAllGroups($this->context->language->id); $sa_groups = array(); foreach($sa_groups_all as $item){ $sa_groups[] = array( 'id' => $item['id_belvg_sa_group'], 'name' => $item['name'] . ' (' . $this->l('based on') . ' ' . $item['ps_attr_name'] .')', 'id_belvg_sa_group' => $item['id_attribute_group'], ); } $this->fields_form = array( 'legend' => array( 'title' => $this->l('Groups'), 'image' => '../img/admin/asterisk.gif' ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Name:'), 'name' => 'name', 'lang' => FALSE, 'size' => 33, 'required' => TRUE, 'hint' => $this->l('Invalid characters:').' <>;=#{}' ), array( 'type' => 'file', 'label' => $this->l('Image:'), 'name' => 'image', 'display_image' => TRUE, 'desc' => $this->l('Upload image from your computer') ), array( 'type' => 'select', 'ajax_type' => 'add', // example of a custom field 'onchange' => 'clear_val()', // example of a custom field 'label' => $this->l('Group:'), 'name' => 'id_belvg_sa_group', 'required' => true, 'options' => array( 'query' => $sa_groups, 'id' => 'id_belvg_sa_group', 'name' => 'name' ), 'desc' => $this->l('Choose the group of the attribute') ), ) ); if (Shop::isFeatureActive()) { $this->fields_form['input'][] = array( 'type' => 'shop', 'label' => $this->l('Shop association:'), 'name' => 'checkBoxShopAsso', ); } $this->fields_form['submit'] = array( 'title' => $this->l(' Save '), 'class' => 'button' ); return $this->renderForm(); } Link to comment Share on other sites More sharing options...
Northman Posted March 28, 2013 Share Posted March 28, 2013 Merci pour ton post ! Link to comment Share on other sites More sharing options...
Valérie Assetskaya Posted March 29, 2013 Author Share Posted March 29, 2013 Je t'en prie Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now