PrestaFrShop Posted February 5, 2022 Share Posted February 5, 2022 Hi, i got a problem i make a module all work when i m in debug mode, but when i put debug off i got error when i edit category in admin an error occurred while editing the category <?php if (!defined('_PS_VERSION_')) { exit; } require_once __DIR__ . '/classes/CategoryFields.php'; use Symfony\Component\Form\AbstractType; use PrestaShopBundle\Form\Admin\Type\SwitchType; class Categorycustomfields extends Module { public function __construct() { $this->name = 'categorycustomfields'; $this->tab = 'others'; $this->version = '0.1.0'; $this->author = 'prestafr'; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Prestafr Category Fields'); $this->description = $this->l('POC : Add category fields without override'); } /** * Installation du module * @return bool */ public function install() { if (!parent::install() || !$this->registerHook([ 'actionCategoryFormBuilderModifier', 'actionAfterCreateCategoryFormHandler', 'actionAfterUpdateCategoryFormHandler', 'filterCategoryContent', ]) || !CategoryFields::installSql() ) { return false; } return true; } /** * Désinstallation du module * @return bool */ public function uninstall() { if ( !parent::uninstall() || !CategoryFields::uninstallSql() ) { return false; } return true; } /** * Ajout de contenu de catégorie sans surcharge * @param array $params * @return array */ public function hookFilterCategoryContent(array $params) { $additional = $this->getCustomCategoryFields($params['object']['id']); if (count($additional)) { $params['object'] = array_merge($params['object'], $additional); return [ 'object' => $params['object'] ]; } } /** * Récupération des informations spécifiques de la catégorie * * @param int $id_category * @return array * @throws PrestaShopDatabaseException * @throws PrestaShopException */ protected function getCustomCategoryFields(int $id_category): array { $return = []; $idCategoryField = CategoryFields::getIdByCategoryId($id_category); if ($idCategoryField) { $categoryField = new CategoryFields($idCategoryField,$this->context->language->id); $presenter = new \PrestaShop\PrestaShop\Adapter\Presenter\Object\ObjectPresenter(); $return = $presenter->present($categoryField); //suppression des champs techniques unset($return['id_category']); unset($return['id']); } return $return; } /** * Modification du formulaire de la catégorie * @param array $params */ public function hookActionCategoryFormBuilderModifier(array $params) { //Pour l'envoi du fichier : regarder ici : https://devdocs.prestashop.com/1.7/modules/sample-modules/extending-sf-form-with-upload-image-field/#introduction try { //Récupération des informations des champs custom $customFieldsValues = $this->getCustomFieldsValue($params['id']); /** @var \Symfony\Component\Form\FormBuilder $formBuilder */ $formBuilder = $params['form_builder']; //Champ standard $formBuilder->add( 'visible_front', SwitchType::class, [ 'label' => $this->l('Visible front'), 'choices' => [ 'OFF' => false, 'ON' => true, ] ] ); $formBuilder->setData($params['data']); } catch ( Exception $e){ } } /** * Action effectuée après la création d'une catégorie * @param array $params * @return void */ public function hookActionAfterCreateCategoryFormHandler(array $params): void { $this->updateData($params['id'], $params['form_data']); } /** * Action effectuée après la mise à jour d'une catégorie * @param array $params * @return void */ public function hookActionAfterUpdateCategoryFormHandler(array $params): void { $this->updateData($params['id'], $params['form_data']); } /** * Récupération des informations spécifique de l'objet * @param int $id_category * @return array */ protected function getCustomFieldsValue(int $id_category): array { try { $idCategoryField = CategoryFields::getIdByCategoryId($id_category); $categoryField = new CategoryFields($idCategoryField); return [ 'visible_front' => $categoryField->visible_front ]; } catch (PrestaShopException $e) { return [ 'visible_front' => 0 ]; } } /** * Fonction qui va effectuer la mise à jour * @param int $id_category * @param array $data * @return void */ protected function updateData(int $id_category, array $data): void { try { $idCategoryField = CategoryFields::getIdByCategoryId($id_category); $categoryField = new CategoryFields($idCategoryField); $categoryField->id_category = $id_category; $categoryField->visible_front = $data['visible_front']; $categoryField->save(); } catch (Exception $e) { $this->log($e->getMessage()); } } /** * Fonction basique de log * @param string $message * @return void */ protected function log($message): void { file_put_contents( dirname(__FILE__) . 'debug.log', date('Y-m-d H:i:s') . $message . "\n", FILE_APPEND ); } } And <?php use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Filesystem; if (!defined('_PS_VERSION_')) { exit; } class CategoryFields extends ObjectModel { /** @var int Object id */ public $id; /** @var int Identifiant de la catégorie prestashop */ public $id_category; /** @var string Code visible_front de la catégorie */ public $visible_front; public static $definition = [ 'table' => 'prestafr_category_field', 'primary' => 'id_category_extra', 'fields' => [ 'id_category' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'length' => 10], 'visible_front' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true], ] ]; /** * Récupération de l'identifiant de l'entité via l'identifiant de catégorie * @param int $id_category * @return false|string|null */ public static function getIdByCategoryId(int $id_category) { return Db::getInstance()->getValue( (new DbQuery()) ->select(self::$definition['primary']) ->from(self::$definition['table']) ->where('id_category=' . $id_category) ); } /** * Installation du modèle * A ajouter dans l'installation du module */ public static function installSql(): bool { try { //Création de la table avec les champs communs $createTable = Db::getInstance()->execute( "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."prestafr_category_field`( `id_category_extra` int(10) NOT NULL AUTO_INCREMENT, `id_category` INT(10) NOT NULL, `visible_front` TINYINT(1) DEFAULT 0 NOT NULL, PRIMARY KEY (`id_category_extra`) ) ENGINE=InnoDB DEFAULT CHARSET=UTF8;" ); } catch (PrestaShopException $e) { return false; } return $createTable; } /** * Suppression des tables du modules * @return bool */ public static function uninstallSql() { return Db::getInstance()->execute("DROP TABLE IF EXISTS "._DB_PREFIX_."prestafr_category_field"); } } Can you help me plz Thanks Link to comment Share on other sites More sharing options...
PrestaFrShop Posted February 6, 2022 Author Share Posted February 6, 2022 Someone plz? 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