herwaldi Posted April 20, 2021 Share Posted April 20, 2021 (edited) Hi, why my module don't save data to sql? Field is visible on sql.. My code module: <?php /* * 2007-2016 PrestaShop * * NOTICE OF LICENSE * * This source file is subject to the Academic Free License (AFL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/afl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to [email protected] so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade PrestaShop to newer * versions in the future. If you wish to customize PrestaShop for your * needs please refer to http://www.prestashop.com for more information. * * @author PrestaShop SA <[email protected]> * @copyright 2007-2016 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_CAN_LOAD_FILES_')) { exit; } use PrestaShop\PrestaShop\Core\Module\WidgetInterface; use Symfony\Component\Validator\Constraints\Url; use Symfony\Component\Validator\Validation; class Ps_Customfields extends Module implements WidgetInterface { private $templateFile; const BLOCK_CUSTOM = [ 'toptext', ]; public function __construct() { $this->name = 'ps_customfields'; $this->author = 'Intermedia'; $this->version = '1.0.0'; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->trans('Custom fields', array(), 'Modules.Customfields.Admin'); $this->description = $this->trans('Custom fields for theme.', array(), 'Modules.Customfields.Admin'); $this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_); $this->templateFile = 'module:ps_customfields/ps_customfields.tpl'; } public function install() { return (parent::install() && Configuration::updateValue('BLOCKCUSTOM_TOPTEXT', '') && $this->registerHook('displayFooter')); } public function uninstall() { return (Configuration::deleteByName('BLOCKCUSTOM_TOPTEXT') && parent::uninstall()); } public function getContent() { if (Tools::isSubmit('submitModule')) { $this->updateFields(); $this->_clearCache('*'); Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&tab_module='.$this->tab.'&conf=4&module_name='.$this->name); } return $this->renderForm(); } public function _clearCache($template, $cache_id = null, $compile_id = null) { parent::_clearCache($this->templateFile); } public function renderForm() { $fields_form = array( 'form' => array( 'legend' => array( 'title' => $this->trans('Settings', array(), 'Admin.Global'), 'icon' => 'icon-cogs' ), 'input' => array( array( 'type' => 'text', 'label' => $this->trans('Top Text', array(), 'Modules.Customfields.Admin'), 'name' => 'blockcustom_toptext', ), ), 'submit' => array( 'title' => $this->trans('Save', array(), 'Admin.Global'), ) ), ); $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $helper->submit_action = 'submitModule'; $helper->tpl_vars = array( 'fields_value' => $this->getConfigFieldsValues(), ); return $helper->generateForm(array($fields_form)); } public function getConfigFieldsValues() { $result = []; foreach (static::BLOCK_CUSTOM as $custom) { $result['blockcustom_' . $custom] = Configuration::get('BLOCKCUSTOM_' . strtoupper($custom)); } return $result; } public function renderWidget($hookName = null, array $configuration = []) { if (!$this->isCached($this->templateFile, $this->getCacheId('ps_customfields'))) { $this->smarty->assign($this->getWidgetVariables($hookName, $configuration)); } return $this->fetch($this->templateFile, $this->getCacheId('ps_customfields')); } public function getWidgetVariables($hookName = null, array $configuration = []) { $block_custom = array(); if ($sf_toptext = Configuration::get('BLOCKCUSTOM_TOPTEXT')) { $block_custom['toptext'] = array( 'label' => $this->trans('Top Text', array(), 'Modules.Customfields.Shop'), 'class' => 'toptext', 'url' => $sf_toptext, ); } return array( 'social_links' => $block_custom, ); } protected function updateFields() { $validator = Validation::createValidator(); $constraints = [new Url()]; foreach (static::BLOCK_CUSTOM as $custom) { $value = Tools::getValue('blockcustom_' . $custom, ''); $violations = $validator->validate($value, $constraints); if (0 === count($violations)) { Configuration::updateValue('BLOCKCUSTOM_' . strtoupper($custom), $value); } } } } Edited April 20, 2021 by herwaldi (see edit history) Link to comment Share on other sites More sharing options...
Rhobur Posted April 21, 2021 Share Posted April 21, 2021 From your getContent method you have to call the postProcess() method where you add your logic for writing to DB. Prestashop specifically says that "POST requests will be managed from the method postProcess()." 1 Link to comment Share on other sites More sharing options...
herwaldi Posted April 22, 2021 Author Share Posted April 22, 2021 Do you have example? I need a simple module with configuration fields. Link to comment Share on other sites More sharing options...
herwaldi Posted April 22, 2021 Author Share Posted April 22, 2021 On 4/21/2021 at 9:21 AM, Rhobur said: From your getContent method you have to call the postProcess() method where you add your logic for writing to DB. Prestashop specifically says that "POST requests will be managed from the method postProcess()." ok bro, add code - its work if (Tools::isSubmit('submitModule')) { foreach (static::CUSTOM_FIELDS as $custom) { $value = Tools::getValue('blockcustom_' . $custom, ''); Configuration::updateValue('BLOCKCUSTOM_' . strtoupper($custom), $value); } $this->_clearCache('*'); Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&tab_module='.$this->tab.'&conf=4&module_name='.$this->name); } but on saving, it doesn't add me <strong> in input Link to comment Share on other sites More sharing options...
Rhobur Posted April 22, 2021 Share Posted April 22, 2021 you might want to encode the html on saving it to table and decode it on retrieving it. 1 Link to comment Share on other sites More sharing options...
herwaldi Posted April 22, 2021 Author Share Posted April 22, 2021 thx, please close topic Link to comment Share on other sites More sharing options...
Rhobur Posted April 22, 2021 Share Posted April 22, 2021 The topic cannot be closed, please edit the first post and add [SOLVED] before the title so other people with a similar issue may find this. 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