SOAMJENA Posted February 20, 2014 Share Posted February 20, 2014 I am new at Prestashop Module development. I am able to create a new module by going through the module development tutorial. I am also able to pass values from backend and also displaying it on frontend. Now i want to pass a comma or enter separated values from a textarea field on admin panel and displaying it through looping on module .tpl file. But I am unable to loop it on module .tpl file. Can you please help me.. Link to comment Share on other sites More sharing options...
vekia Posted February 20, 2014 Share Posted February 20, 2014 can you show an example of textarea field contents + your foreach loop please? Link to comment Share on other sites More sharing options...
SOAMJENA Posted February 21, 2014 Author Share Posted February 21, 2014 can you show an example of textarea field contents + your foreach loop please? array( 'type' => 'textarea', 'label' => $this->l('Sample Message'), 'name' => 'MYMODULE_TEXT', 'size' => 20, 'required' => false ), 1. Above is the code on modules/mymodule/mymodule.php for a textarea field on module backend.. 2. And I pass a comma separated entry on backend e.g (Content1, Content2, Content3) throgh the varibale $my_module_message 3. I want to separate above comma separated string (Which is coming through variable $my_module_message) by looping on modules/mymodule/mymodule.tpl file.. Can you please help me which loop is essential on smarty tpl file because (Php for loop and explode function does not work.) Please provide me the right syntax.. Link to comment Share on other sites More sharing options...
vekia Posted February 21, 2014 Share Posted February 21, 2014 why not to explode it with php, then pass variable (array) to smarty array then you will be able to do on this variable foreach loop Link to comment Share on other sites More sharing options...
SOAMJENA Posted February 24, 2014 Author Share Posted February 24, 2014 Can you please provide me the explode function and foreach loop example (partial code) which is to be used on mymodule.tpl file for separating the variable mention on previous comment. Link to comment Share on other sites More sharing options...
vekia Posted February 24, 2014 Share Posted February 24, 2014 php code: $string="value1,value2,value3,value4,value5,value6"; $array=explode(",",$string); //now it's time to pass $array to smarty as myArray variable: this->context->smarty->assign('myArray',$array); in template file: {foreach $myArray as $item} {$item}<br/> {foreach} as a resut you will see: value1<br/> value2<br/> value3<br/> value4<br/> value5<br/> value6<br/> Link to comment Share on other sites More sharing options...
SOAMJENA Posted February 24, 2014 Author Share Posted February 24, 2014 Hi, And Template File mymodule.tpl file code (partial) <?php $string="value1,value2,value3,value4,value5,value6"; $array=explode(",",$string); ?> //now it's time to pass $array to smarty as myArray variable: $this->context->smarty->assign('myArray',$array); {foreach $myArray as $item} {$item}<br/> {/foreach} But it output the below error on Prestashop frontend (where module is activated) and page is blank. //now it's time to pass $array to smarty as myArray variable: $this->context->smarty->assign('myArray',$array); Undefined index: myArray (C:\wamp\www\prestashop\cache\smarty\compile\02\32\e0\0232e0d5e05ec048f66eb5cf084e7071c2fe8ec2.file.mymodule.tpl.php, line 55) Link to comment Share on other sites More sharing options...
SOAMJENA Posted February 24, 2014 Author Share Posted February 24, 2014 <?php $string="value1,value2,value3,value4,value5,value6"; $array=explode(",",$string); ?> In which file this code to be placed mymodule.php or mymodule.tpl???? If I placed that on mymodule.tpl file then it just comment out on fronted. No output results. Link to comment Share on other sites More sharing options...
vekia Posted February 24, 2014 Share Posted February 24, 2014 you cant use <?php ?> code in smarty template file you have to use this code in controller (php file) Link to comment Share on other sites More sharing options...
SOAMJENA Posted February 25, 2014 Author Share Posted February 25, 2014 Now I pass varible on mymodule.php My mymodule.php code(partial) public function hookDisplayLeftColumn($params) { $this->context->smarty->assign( array( 'my_module_name1' => Configuration::get('MYMODULE_NAME'), 'my_module_link' => Configuration::get('MYMODULE_LINK'), 'my_module_message' => Configuration::get('MYMODULE_TEXT') ) ); $string="".$my_module_name1.""; $array1=explode(",",$string); $this->context->smarty->assign('myArray',$array1); return $this->display(__FILE__, 'mymodule.tpl'); } public function hookDisplayRightColumn($params) { return $this->hookDisplayLeftColumn($params); } public function hookDisplayHome($params) { $this->context->smarty->assign( array( 'my_module_name1' => Configuration::get('MYMODULE_NAME'), 'my_module_link' => Configuration::get('MYMODULE_LINK'), 'my_module_message' => Configuration::get('MYMODULE_TEXT') ) ); $string="".$my_module_name1.""; $array1=explode(",",$string); $this->context->smarty->assign('myArray',$array1); return $this->display(__FILE__, 'mymodule.tpl'); } It returns below error on backend.. Undefined variable: my_module_name1 (C:\wamp\www\prestashop\modules\mymodule\mymodule.php, line 52) Link to comment Share on other sites More sharing options...
vekia Posted February 25, 2014 Share Posted February 25, 2014 this variable doesn't exist. define it first before $string="".$my_module_name1.""; Link to comment Share on other sites More sharing options...
SOAMJENA Posted February 26, 2014 Author Share Posted February 26, 2014 Hi, Now My mymodule.php code (partial code) public function hookDisplayLeftColumn($params) { $this->context->smarty->assign( array( 'my_module_name1' => Configuration::get('MYMODULE_NAME'), 'my_module_link' => Configuration::get('MYMODULE_LINK'), 'my_module_message' => Configuration::get('MYMODULE_TEXT') ) ); $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); $string="".$my_module_message.""; $array1=explode(",",$string); $this->context->smarty->assign('myArray',$array1); return $this->display(__FILE__, 'mymodule.tpl'); } public function hookDisplayRightColumn($params) { return $this->hookDisplayLeftColumn($params); } public function hookDisplayHome($params) { $this->context->smarty->assign( array( 'my_module_name1' => Configuration::get('MYMODULE_NAME'), 'my_module_link' => Configuration::get('MYMODULE_LINK'), 'my_module_message' => Configuration::get('MYMODULE_TEXT') ) ); $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); $string="".$my_module_message.""; $array1=explode(",",$string); $this->context->smarty->assign('myArray',$array1); return $this->display(__FILE__, 'mymodule.tpl'); } And my template file (mymodule.tpl code partial) {foreach $myArray as $item} {$item}<br/> {/foreach} But it gives no output on frontend. I can't understand where is the wrong... Please help me. Below also atached my mymodule.php file mymodule.php Link to comment Share on other sites More sharing options...
vekia Posted February 26, 2014 Share Posted February 26, 2014 where you've got this: "MYMODULE_TEXT" ?, regarding to the: strval(Tools::getValue('MYMODULE_TEXT')); it is posted somewhere? Link to comment Share on other sites More sharing options...
SOAMJENA Posted February 28, 2014 Author Share Posted February 28, 2014 $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); I use this line to get the value from module back end parameter. http://doc.prestashop.com/display/PS14/Creating+a+PrestaShop+module I follow above Prestashop Module Development Tutorial. Link to comment Share on other sites More sharing options...
vekia Posted February 28, 2014 Share Posted February 28, 2014 Tools::getValue('MYMODULE_TEXT') this code returns a value from POST or from GET variable you form must be posted, or variable must be included to $_GET array. Link to comment Share on other sites More sharing options...
SOAMJENA Posted March 4, 2014 Author Share Posted March 4, 2014 (edited) Ya.. The form is submitted.. $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); Here attached the mymodule.php file. Below is the code for mymodule.tpl file. <!-- Block mymodule --> <div id="mymodule_block_left" class="block"> <h4>Welcome!</h4> <div class="block_content"> <p>Hello, {if isset($my_module_name1) && $my_module_name1} {$my_module_name1} {else} World {/if} ! </p> {foreach $myArray as $item} {$item}<br/> {/foreach} <ul> <li><a href="{$my_module_link}" title="Click this link">Click me!</a></li> </ul> </div> </div> <!-- /Block mymodule --> please give a look on my attached mymodule.php file.. I don't understand where I am wrong.. Because I follow everything you mention on earlier comments. but still this time no result output. mymodule.php Edited March 4, 2014 by SOAMJENA (see edit history) Link to comment Share on other sites More sharing options...
SOAMJENA Posted March 4, 2014 Author Share Posted March 4, 2014 I think there is a problem on my mymodule.php file. I can't able to define the varibale $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); in the right position which fetch value (comma separated) from backend. Please advice me or make changes on my mymodule.php file which I sent on previous comment and send it here. So I can understand the syntax and utilize that. Link to comment Share on other sites More sharing options...
vekia Posted March 4, 2014 Share Posted March 4, 2014 i already said it Tools::getValue('MYMODULE_TEXT') this code returns a value from POST or from GET variable you form must be posted, or variable must be included to $_GET array. Link to comment Share on other sites More sharing options...
SOAMJENA Posted March 5, 2014 Author Share Posted March 5, 2014 I understand that Tools::getValue('MYMODULE_TEXT') this code returns a value from POST or from GET variable But I am unable to pass that variable through submit.. Please give a look on my mymodule.php file which I send on previous comment. And advice me where I am wrong and what to do to get that. Please provide me correct full mymodule.php file code with example how to pass $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); that varible with form submission and explode it through.. Link to comment Share on other sites More sharing options...
SOAMJENA Posted March 5, 2014 Author Share Posted March 5, 2014 This time my mymodule.php code public function hookDisplayHome($params) { $this->context->smarty->assign( array( 'my_module_name1' => Configuration::get('MYMODULE_NAME'), 'my_module_link' => Configuration::get('MYMODULE_LINK'), 'my_module_message' => Configuration::get('MYMODULE_TEXT') ) ); if (Tools::isSubmit('submit'.$this->name)) { $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); } $string="".$my_module_message.""; $array1=explode(",",$string); $this->context->smarty->assign('myArray',$array1); return $this->display(__FILE__, 'mymodule.tpl'); } I pass that $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); through form submission still no output result. Link to comment Share on other sites More sharing options...
vekia Posted March 5, 2014 Share Posted March 5, 2014 and where you've got this form? please show code of it Link to comment Share on other sites More sharing options...
SOAMJENA Posted March 5, 2014 Author Share Posted March 5, 2014 Here is my entire mymodule.php code Please guide me where i am wrong. I can't able to print $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); through for loop on template file. <?php if (!defined('_PS_VERSION_')) exit; class MyModule extends Module { public function __construct() { $this->name = 'mymodule'; $this->tab = 'front_office_features'; $this->version = 1.0; $this->author = 'Qubesys Technologies Pvt.Ltd'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('My module'); $this->description = $this->l('Description of my module.'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); if (!Configuration::get('MYMODULE_NAME')) $this->warning = $this->l('No name provided'); } public function install() { if (Shop::isFeatureActive()) Shop::setContext(Shop::CONTEXT_ALL); return parent::install() && $this->registerHook('leftColumn') && $this->registerHook('header') && Configuration::updateValue('MYMODULE_NAME', 'my friend'); Configuration::updateValue('MYMODULE_LINK', 'http://www.google.com'); Configuration::updateValue('MYMODULE_TEXT', 'test'); } public function hookDisplayLeftColumn($params) { $this->context->smarty->assign( array( 'my_module_name1' => Configuration::get('MYMODULE_NAME'), 'my_module_link' => Configuration::get('MYMODULE_LINK'), 'my_module_message' => Configuration::get('MYMODULE_TEXT') ) ); return $this->display(__FILE__, 'mymodule.tpl'); } public function hookDisplayRightColumn($params) { return $this->hookDisplayLeftColumn($params); } public function hookDisplayHome($params) { $this->context->smarty->assign( array( 'my_module_name1' => Configuration::get('MYMODULE_NAME'), 'my_module_link' => Configuration::get('MYMODULE_LINK'), 'my_module_message' => Configuration::get('MYMODULE_TEXT') ) ); if (Tools::isSubmit('submit'.$this->name)) { $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); } $string="".$my_module_message.""; $array1=explode(",",$string); $this->context->smarty->assign('myArray',$array1); return $this->display(__FILE__, 'mymodule.tpl'); } public function hookDisplayHeader() { $this->context->controller->addCSS($this->_path.'css/mymodule.css', 'all'); } public function uninstall() { if (!parent::uninstall()) Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'mymodule`'); parent::uninstall(); } public function getContent() { $output = null; if (Tools::isSubmit('submit'.$this->name)) { $my_module_name1 = strval(Tools::getValue('MYMODULE_NAME')); $my_module_link = strval(Tools::getValue('MYMODULE_LINK')); $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); if (!$my_module_name1 || empty($my_module_name1) || !Validate::isGenericName($my_module_name1) ) $output .= $this->displayError( $this->l('Invalid Configuration value') ); else { Configuration::updateValue('MYMODULE_NAME', $my_module_name1); Configuration::updateValue('MYMODULE_LINK', $my_module_link); Configuration::updateValue('MYMODULE_TEXT', $my_module_message); $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('Settings'), ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Configuration value'), 'name' => 'MYMODULE_NAME', 'size' => 20, 'required' => true ), array( 'type' => 'text', 'label' => $this->l('Sample Message'), 'name' => 'MYMODULE_TEXT', 'size' => 20, 'required' => false ), array( 'type' => 'text', 'label' => $this->l('Module Link'), 'name' => 'MYMODULE_LINK', 'size' => 40, 'required' => false ) ), 'submit' => array( 'title' => $this->l('Save'), 'class' => 'button' ) ); $helper = new HelperForm(); // Module, t oken 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_NAME'] = Configuration::get('MYMODULE_NAME'); $helper->fields_value['MYMODULE_TEXT'] = Configuration::get('MYMODULE_TEXT'); $helper->fields_value['MYMODULE_LINK'] = Configuration::get('MYMODULE_LINK'); return $helper->generateForm($fields_form); } } ?> Link to comment Share on other sites More sharing options...
vekia Posted March 5, 2014 Share Posted March 5, 2014 it's a php file, not module template (.tpl) file Link to comment Share on other sites More sharing options...
SOAMJENA Posted March 6, 2014 Author Share Posted March 6, 2014 Here is the module template file code (mymodule.tpl file) <!-- Block mymodule --> <div id="mymodule_block_left" class="block"> <h4>Welcome!</h4> <div class="block_content"> <p>Hello, {if isset($my_module_name1) && $my_module_name1} {$my_module_name1} {else} World {/if} ! </p> {foreach $myArray as $item} {$item}<br/> {/foreach} {$my_module_message} <ul> <li><a href="{$my_module_link}" title="Click this link">Click me!</a></li> </ul> </div> </div> <!-- /Block mymodule --> Link to comment Share on other sites More sharing options...
vekia Posted March 6, 2014 Share Posted March 6, 2014 there is no <form> so variable isn't posted to your hook. this is why it doesnt work. as i said several times before Tools::getValue uses $_POST and $_GET variables Link to comment Share on other sites More sharing options...
SOAMJENA Posted March 10, 2014 Author Share Posted March 10, 2014 Is there any other way to pass a dynamic variable (which contains the value form admin module area) through hook. Can you please provide a short example or code how to do that. Link to comment Share on other sites More sharing options...
vekia Posted March 10, 2014 Share Posted March 10, 2014 let's clarify take alook, you use this code: public function hookDisplayHome($params) { $this->context->smarty->assign( array( 'my_module_name1' => Configuration::get('MYMODULE_NAME'), 'my_module_link' => Configuration::get('MYMODULE_LINK'), 'my_module_message' => Configuration::get('MYMODULE_TEXT') ) ); if (Tools::isSubmit('submit'.$this->name)) { $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); } $string="".$my_module_message.""; $array1=explode(",",$string); $this->context->smarty->assign('myArray',$array1); return $this->display(__FILE__, 'mymodule.tpl'); } it's a Hook function, this part of code is only "executed" when you're browsing front office of your store. inside function you've got this code: if (Tools::isSubmit('submit'.$this->name)){ $my_module_message = strval(Tools::getValue('MYMODULE_TEXT')); } it is executed only in you will SUBMIT form from front office, in addition variable: strval(Tools::getValue('MYMODULE_TEXT')); must be also submitted because as i said before Tools::getValue functins gets variables from $_GET / $_PARAM variables. what to do? you have to change it a bit, if you store somewhere contents of $my_module_message you have to use Configuration::get(); function (if you store it in configuration table) Link to comment Share on other sites More sharing options...
SOAMJENA Posted March 10, 2014 Author Share Posted March 10, 2014 Thanks now i got it. public function hookDisplayHome($params) { $this->context->smarty->assign( array( 'my_module_name1' => Configuration::get('MYMODULE_NAME'), 'my_module_link' => Configuration::get('MYMODULE_LINK'), 'my_module_message' => Configuration::get('MYMODULE_TEXT') ) ); $my_module_message = Configuration::get('MYMODULE_TEXT'); $string="".$my_module_message.""; $array1=explode(",",$string); $this->context->smarty->assign('myArray',$array1); return $this->display(__FILE__, 'mymodule.tpl'); } This code works for me.. Thank You Again... Link to comment Share on other sites More sharing options...
Recommended Posts