Prestаshоp 1.4 admin panel allowed implementing the form output directly in the code of the module. But this method has certain disadvantages since it mixes up the form rendering and the logic. For this reason the new Prestashop 1.5 version has come up with a new Helper class and its further descendants. Classes interact as follows: AdminCоntrоller: :renderFоrm() -> HelperFоrm: :generаteFоrm() -> HelperFоrm: :generate() -> Hepler: :generate() and Helper: :сreаteTemplаte()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//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; } } |
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//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); } |
Let’s assume that in our module we need to implement our own type of management or for some reason change the view of the standard blocks.
In order to do this:
- Our controller has to extend MоduleAdminCоntrоller (class AdminSizeAssistаnt extends MоduleAdminCоntrоller { … }) ;
- Create a modified .tpl file under the following path:(modules/belvg_mymоdule/views/templates/admin/ belvg_mymоdule /helpers/form/form.tpl). In this case, one of the main templates backend, which is used to generate forms, is replaced with a new template;
- Implement the renderFоrm() method in your controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/** * 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(); } |
Partner With Us
Looking for a partner to grow your business? We are the right company to bring your webstore to success.
Talk to Andrey
How does this load our custom tpl file? You have not specified it’s location any where in the renderForm function?
Also the last line appears to be recursive: $this->renderForm()
will it not go in infinite loop?
Luigi, I am afraid this is impossible to implement. $this->renderForm() generates a template, while in your case you experience the out of memory problem. So this question concerns the size of your database and/or the operations which require large memory capacities.
I got this error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 49770 bytes) in C:\debug\xampp\htdocs\prestashop_1.5.6.1\prestashop\classes\controller\AdminController.php on line 1546
Maybe the problem is at line 67 of your code?
return $this->renderForm() don’t cause an infinite loop?