Hi guys,
I have taken up a challenge to create a simple module for Prestashop 1.7.8. My goal is to create a custom boolean field to group categories. I have come so far that I can see the field on admin page of each category but it isn't showing the same value as in the database. When I click on save the category status changes to disabled.
Here my entire code:
if (!defined('_PS_VERSION_')) { exit; } use PrestaShopBundle\Form\Admin\Type\SwitchType; class New_custom_boolean extends Module{ public function __construct() { $this->name = 'new_custom_boolean'; $this->tab = 'administration'; $this->version = '1.0.0'; $this->author = 'no name modules'; $this->bootstrap = true; $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('New custom boolean'); $this->description = $this->l('New custom boolean field'); } public function install() { if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_ALL); } if (!parent::install() || !$this->registerHook([ 'actionCategoryFormBuilderModifier', 'actionAfterCreateCategoryFormHandler', 'actionAfterUpdateCategoryFormHandler', ]) ) { return false; } return true; } public function uninstall() { Configuration::updateValue('NEW_CUSTOM_BOOLEAN_SETTINGS', serialize(array(true, true, false))); $configuration_array = unserialize(Configuration::get('NEW_CUSTOM_BOOLEAN__SETTINGS')); if (!parent::uninstall() || !Configuration::deleteByName('NEW_CUSTOM_BOOLEAN_NAME') ) { return false; } return true; } public function hookActionCategoryFormBuilderModifier(array $params) { $formBuilder = $params['form_builder']; $formBuilder->add( $this->name.'new_custom_boolean', SwitchType::class, [ 'label' => $this->l('Turn on or off'), 'required' => false, 'choices' => [ $this->l('Enabled') => 1, $this->l('Disabled') => 0, ], ] ); $params['data'][$this->name.'new_custom_boolean'] = 'new_custom_boolean'; $params['data']['active'] = false; $formBuilder->setData($params['data']); } public function hookActionAfterCreateCategoryFormHandler(array $params) { $this->updateData($params['form_data'], $params); } public function hookActionAfterUpdateCategoryFormHandler(array $params) { $this->updateData($params['form_data'], $params); } protected function updateData(array $data, $params) { $cat = new Category((int)$params['id']); $cat->new_custom_boolean = $data['new_custom_boolean']; $cat->update(); } }
I have added a field to presta_category_lang new_custom_boolean. Can someone help me solve this.
Best regards.