Carpe_diem Posted December 6, 2022 Share Posted December 6, 2022 (edited) 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. Edited December 7, 2022 by Carpe_diem Solved (see edit history) Link to comment Share on other sites More sharing options...
ventura Posted December 6, 2022 Share Posted December 6, 2022 it will be necessary to add the new field in class Category.php Link to comment Share on other sites More sharing options...
Carpe_diem Posted December 6, 2022 Author Share Posted December 6, 2022 14 minutes ago, ventura said: it will be necessary to add the new field in class Category.php Do I edit the Core Category.php or do I create an own Category.php in my module like new_module_boolean/classes/category.php Link to comment Share on other sites More sharing options...
ventura Posted December 6, 2022 Share Posted December 6, 2022 Try it with something like that, new created field name custom_boolean Create a class override in module_name/override/classes/Category.php <?php class Category extends CategoryCore { public $custom_boolean; public function __construct($idCategory = null, $idLang = null, $idShop = null){ self::$definition['fields']['custom_boolean'] = array('type' => self::TYPE_BOOL, 'validate' => 'isBool'); parent::__construct($idCategory, $idLang, $idShop); } } Actiion hooks something like that public function hookActionCategoryFormBuilderModifier(array $params) { $shopId = $this->context->shop->id; $categoryId = $params['id']; $langId = $this->context->language->id; $category = new Category( $categoryId, $langId, $shopId); $formBuilder = $params['form_builder']; $formBuilder->add( 'custom_boolean', SwitchType::class, [ 'label' => $this->l('Turn on or off'), 'required' => false, 'choices' => [ $this->l('Disabled') => 0, $this->l('Enabled') => 1, ], ] ); $params['data']['custom_boolean'] = $category->custom_boolean; $formBuilder->setData($params['data']); } public function hookActionAfterUpdateCategoryFormHandler(array $params) { $this->updateData($params); } public function hookActionAfterCreateCategoryFormHandler(array $params) { $this->updateData($params); } private function updateData(array $params) { $formData = $params['form_data']; $cat = new Category((int)$params['id']); $cat->custom_boolean = $formData['custom_boolean']; $cat->update(); } Link to comment Share on other sites More sharing options...
Carpe_diem Posted December 6, 2022 Author Share Posted December 6, 2022 (edited) 27 minutes ago, ventura said: Try it with something like that, new created name custom_boolean Create a class override in module_name/override/classes/Category.php <?php class Category extends CategoryCore { public $custom_boolean; public function __construct($id_category = null, $id_lang = null, $id_shop = null){ self::$definition['fields']['custom_boolean'] =['type' => self::TYPE_BOOL, 'validate' => 'isBool']; parent::__construct($id_category, $id_lang, $id_shop); } } Actiion hooks something like that public function hookActionCategoryFormBuilderModifier(array $params) { $shopId = $this->context->shop->id; $categoryId = $params['id']; $langId = $this->context->language->id; $category = new Category( $categoryId, $langId, $shopId); $formBuilder = $params['form_builder']; $formBuilder->add( 'custom_boolean', SwitchType::class, [ 'label' => $this->l('Turn on or off'), 'required' => false, 'choices' => [ $this->l('Disabled') => 0, $this->l('Enabled') => 1, ], ] ); $params['data']['custom_boolean'] = $category->custom_boolean; $formBuilder->setData($params['data']); } public function hookActionAfterUpdateCategoryFormHandler(array $params) { $this->updateData($params); } public function hookActionAfterCreateCategoryFormHandler(array $params) { $this->updateData($params); } private function updateData(array $params) { $formData = $params['form_data']; $cat = new Category((int)$params['id']); $cat->custom_boolean = $formData['custom_boolean']; $cat->update(); } I am halfway there. With this code I get the correct value from the DB but when I change the value I and click on Save I get an error: An error occurred while editing the category. When I turned on debug mode I get this error An unexpected error occurred. [Symfony\Component\Debug\Exception\ContextErrorException code 0]: Notice: Undefined property: Category::$custom_boolean Edited December 6, 2022 by Carpe_diem (see edit history) Link to comment Share on other sites More sharing options...
ventura Posted December 6, 2022 Share Posted December 6, 2022 It seems as if it does not recognize the override. Clear the installation cache Link to comment Share on other sites More sharing options...
Carpe_diem Posted December 6, 2022 Author Share Posted December 6, 2022 I have cleared cache and even re-installed the module but the result is the same. Can't figure out what is wrong. Link to comment Share on other sites More sharing options...
ventura Posted December 6, 2022 Share Posted December 6, 2022 Try the modified code from the override in the previous post Link to comment Share on other sites More sharing options...
Carpe_diem Posted December 7, 2022 Author Share Posted December 7, 2022 I uninstalled the module and deleted the folder and re-created it. I added my install and uninstall methods and copied the code you sent in previous posts. Now when I save I get this warning [Symfony\Component\Debug\Exception\ContextErrorException code 0]: Warning: preg_match() expects parameter 2 to be string, array given Link to comment Share on other sites More sharing options...
ventura Posted December 7, 2022 Share Posted December 7, 2022 check the override class file in /override/classes/Category.php Link to comment Share on other sites More sharing options...
Carpe_diem Posted December 7, 2022 Author Share Posted December 7, 2022 I changed it to the class to: class Category extends CategoryCore { public $custom_boolean; public function __construct($id_category = null, $id_lang = null, $id_shop = null) { $definition = self::$definition; $definition['fields']['custom_boolean'] = array( 'type' => self::TYPE_BOOL, 'validate' => 'isBool' ); parent::__construct($id_category, $id_lang, $id_shop); } } Now the error is gone when I save, but the field value doesn't update in the database. Link to comment Share on other sites More sharing options...
ventura Posted December 7, 2022 Share Posted December 7, 2022 Remove field in override/classes / Category.php Try it with catcustombol.zip 1 Link to comment Share on other sites More sharing options...
Carpe_diem Posted December 7, 2022 Author Share Posted December 7, 2022 @ventura thank you very much for the help, I wouldn't solve it without your help. 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