SheldonCooper Posted September 16, 2022 Share Posted September 16, 2022 I want to override Productinformation.php file which is in SRC folder. Is it possible to override the file ? If yes, How ? Link to comment Share on other sites More sharing options...
ps8modules Posted September 16, 2022 Share Posted September 16, 2022 (edited) And can you be specific about what you need to rewrite? Have you discovered the Product.php override and the hookActionProductFormBuilderModifier yet? e.g.: use Symfony\Component\Form\Extension\Core\Type as FormType; use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType; use PrestaShopBundle\Form\Admin\Type\TranslateType; public function hookActionProductFormBuilderModifier($params) { $formBuilder = $params['form_builder']; $allFields = $formBuilder->all(); foreach ($allFields as $inputField => $input) { $formBuilder->add($input); if ($inputField == 'reference') { $formBuilder->add( 'custom_reference', TextType::class, ['label' => 'Custom reference'] ); } } } Edited September 16, 2022 by 4you.software (see edit history) Link to comment Share on other sites More sharing options...
SheldonCooper Posted September 16, 2022 Author Share Posted September 16, 2022 (edited) I want to save the Product's name with a special character, for that first i just removed the validation from product class file but it didn't work. SO, i make changes in validate.php and productinformation.php file and it works by changing in core files. But i want to override the src file i.e, productinformation.php so that i don't need to make changes in core files. If there is any other solutions available for it ,please let me know. Thanks Edited September 16, 2022 by SheldonCooper (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted September 16, 2022 Share Posted September 16, 2022 (edited) This hook is for override !!! Remove field from hook hookActionProductFormBuilderModifier and add new: use Language; use PrestaShop\PrestaShop\Adapter\Category\CategoryDataProvider; use PrestaShop\PrestaShop\Adapter\Configuration; use PrestaShop\PrestaShop\Adapter\Feature\FeatureDataProvider; use PrestaShop\PrestaShop\Adapter\LegacyContext; use PrestaShop\PrestaShop\Adapter\Manufacturer\ManufacturerDataProvider; use PrestaShop\PrestaShop\Adapter\Product\ProductDataProvider; use PrestaShop\PrestaShop\Core\Domain\Product\ProductSettings; use PrestaShopBundle\Form\Admin\Category\SimpleCategory; use PrestaShopBundle\Form\Admin\Feature\ProductFeature; use PrestaShopBundle\Form\Admin\Type\ChoiceCategoriesTreeType; use PrestaShopBundle\Form\Admin\Type\CommonAbstractType; use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType; use PrestaShopBundle\Form\Admin\Type\TranslateType; use PrestaShopBundle\Form\Admin\Type\TypeaheadProductCollectionType; use PrestaShopBundle\Form\Admin\Type\TypeaheadProductPackCollectionType; use PrestaShopBundle\Form\Validator\Constraints\TinyMceMaxLength; use PrestaShopBundle\Service\Routing\Router; use Symfony\Component\Form\Extension\Core\Type as FormType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Validator\Constraints as Assert; class my_module extends Module { public function __construct() { ... ... } public function install() { ... $this->registerHook('actionProductFormBuilderModifier'); } public function uninstall() { ... $this->unregisterHook('actionProductFormBuilderModifier'); } public function hookActionProductFormBuilderModifier($params) { $formBuilder = $params['form_builder']; $formBuilder->remove('name'); // remove old name input // create new name filed $formBuilder->add('name', TranslatableType::class, [ 'type' => FormType\TextType::class, 'options' => [ 'constraints' => [ new Assert\NotBlank(), new Assert\Length(['min' => 3, 'max' => 128]), ], 'attr' => [ 'placeholder' => $this->translator->trans('Enter your product name', [], 'Admin.Catalog.Help'), 'class' => 'edit js-edit serp-default-title', ], ], ]); } } Edited September 16, 2022 by 4you.software (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted September 16, 2022 Share Posted September 16, 2022 To change the position you can use: $formBuilder->addAfter or $formBuilder->addBefore You can find information in ./src/PrestaShopBundle/Form/FormBuilderModifier.php Link to comment Share on other sites More sharing options...
SheldonCooper Posted September 16, 2022 Author Share Posted September 16, 2022 @4you.software Getting this error: Attempted to load class "ProductSettings" from the global namespace. Did you forget a "use" statement for "PrestaShop\PrestaShop\Core\Domain\Product\ProductSettings"? Link to comment Share on other sites More sharing options...
ps8modules Posted September 16, 2022 Share Posted September 16, 2022 So add it to the use section. He's writing to you. Link to comment Share on other sites More sharing options...
ps8modules Posted September 16, 2022 Share Posted September 16, 2022 (edited) use exists: Edited September 16, 2022 by 4you.software (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted September 16, 2022 Share Posted September 16, 2022 Your version of Prestashop is missing from the beginning !!! Versions may have a different path. Link to comment Share on other sites More sharing options...
ps8modules Posted September 16, 2022 Share Posted September 16, 2022 Put the whole code here, it's impossible to find what you changed where, forgot, etc. Link to comment Share on other sites More sharing options...
SheldonCooper Posted September 16, 2022 Author Share Posted September 16, 2022 <?php use Language; use PrestaShop\PrestaShop\Adapter\Category\CategoryDataProvider; use PrestaShop\PrestaShop\Adapter\Configuration; use PrestaShop\PrestaShop\Adapter\Feature\FeatureDataProvider; use PrestaShop\PrestaShop\Adapter\LegacyContext; use PrestaShop\PrestaShop\Adapter\Manufacturer\ManufacturerDataProvider; use PrestaShop\PrestaShop\Adapter\Product\ProductDataProvider; use PrestaShop\PrestaShop\Core\Domain\Product\ProductSettings; use PrestaShopBundle\Form\Admin\Category\SimpleCategory; use PrestaShopBundle\Form\Admin\Feature\ProductFeature; use PrestaShopBundle\Form\Admin\Type\ChoiceCategoriesTreeType; use PrestaShopBundle\Form\Admin\Type\CommonAbstractType; use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType; use PrestaShopBundle\Form\Admin\Type\TranslateType; use PrestaShopBundle\Form\Admin\Type\TypeaheadProductCollectionType; use PrestaShopBundle\Form\Admin\Type\TypeaheadProductPackCollectionType; use PrestaShopBundle\Form\Validator\Constraints\TinyMceMaxLength; use PrestaShopBundle\Service\Routing\Router; use Symfony\Component\Form\Extension\Core\Type as FormType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Validator\Constraints as Assert; class Customproductname extends Module { public function __construct() { $this->name = 'customproductname'; $this->tab = 'administration'; $this->version = '1.7.8'; $this->author = 'Nethues'; $this->need_instance = 0; /** * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6) */ $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Custom Validation'); $this->description = $this->l('Allow product\'s to have special characters in their name'); $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); } public function install() { return parent::install() && $this->registerHook('actionProductFormBuilderModifier'); } public function uninstall() { return parent::uninstall() && $this->unregisterHook('actionProductFormBuilderModifier'); } public function hookActionProductFormBuilderModifier($params) { $formBuilder = $params['form_builder']; $formBuilder->remove('name'); // remove old name input // create new name filed $formBuilder->add('name', TranslatableType::class, [ 'type' => FormType\TextType::class, 'options' => [ 'constraints' => [ new Assert\NotBlank(), new Assert\Length(['min' => 3, 'max' => 128]), ], 'attr' => [ 'placeholder' => $this->translator->trans('Enter your product name', [], 'Admin.Catalog.Help'), 'class' => 'edit js-edit serp-default-title', ], ], ]); } } Link to comment Share on other sites More sharing options...
SheldonCooper Posted September 16, 2022 Author Share Posted September 16, 2022 @4you.software Let me know if i'm missing something. Link to comment Share on other sites More sharing options...
ps8modules Posted September 20, 2022 Share Posted September 20, 2022 hi I checked the code and everything is fine with me. There is still one option, overwriting during installation and reverting changes when uninstalling your module. Link to comment Share on other sites More sharing options...
ps8modules Posted September 20, 2022 Share Posted September 20, 2022 sample: public function install() { ... $this->backupProductInformationFile(); ... } public function uninstall() { ... $this->restoreProductInformationFile(); ... } public function backupProductInformationFile() { $backupDir = _PS_MODULE_DIR_.$this->name.'/backup'; if (!file_exists($backupDir) { mkdir($backupDir, 0755); } $backupFileName = 'ProductInformation.php'; $backupFile = _PS_ROOT_DIR_.'/src/PrestaShopBundle/Form/Admin/Product/'.$backupFileName; if (file_exists($backupFile) { $backupFile = file_get_contents($backupFile); file_put_contents($backupDir.'/'.$backupFileName, $backupFile); // replace text in original file $overideFileText = str_replace('/[<>;=#{}]/', '', $backupFile); // save replaced file file_put_contents($backupFile, $overideFileText); } } public function restoreProductInformationFile() { $backupDir = _PS_MODULE_DIR_.$this->name.'/backup'; $backupFileName = 'ProductInformation.php'; $originalFilePath = _PS_ROOT_DIR_.'/src/PrestaShopBundle/Form/Admin/Product/'.$backupFileName; $restoreFilePath = $backupDir.'/'.$backupFileName; if (file_exists($restoreFilePath) { $restoreFile = file_get_contents($restoreFilePath); file_put_contents($originalFilePath, $restoreFilePath); unlink($restoreFilePath); } } Link to comment Share on other sites More sharing options...
SheldonCooper Posted September 20, 2022 Author Share Posted September 20, 2022 3 hours ago, 4you.software said: sample: public function install() { ... $this->backupProductInformationFile(); ... } public function uninstall() { ... $this->restoreProductInformationFile(); ... } public function backupProductInformationFile() { $backupDir = _PS_MODULE_DIR_.$this->name.'/backup'; if (!file_exists($backupDir) { mkdir($backupDir, 0755); } $backupFileName = 'ProductInformation.php'; $backupFile = _PS_ROOT_DIR_.'/src/PrestaShopBundle/Form/Admin/Product/'.$backupFileName; if (file_exists($backupFile) { $backupFile = file_get_contents($backupFile); file_put_contents($backupDir.'/'.$backupFileName, $backupFile); // replace text in original file $overideFileText = str_replace('/[<>;=#{}]/', '', $backupFile); // save replaced file file_put_contents($backupFile, $overideFileText); } } public function restoreProductInformationFile() { $backupDir = _PS_MODULE_DIR_.$this->name.'/backup'; $backupFileName = 'ProductInformation.php'; $originalFilePath = _PS_ROOT_DIR_.'/src/PrestaShopBundle/Form/Admin/Product/'.$backupFileName; $restoreFilePath = $backupDir.'/'.$backupFileName; if (file_exists($restoreFilePath) { $restoreFile = file_get_contents($restoreFilePath); file_put_contents($originalFilePath, $restoreFilePath); unlink($restoreFilePath); } } I might be doing something wrong, meanwhile i have attached my module. If you find any thing which need to change let me know. Thanks for everything.🙂 customproductname.zip Link to comment Share on other sites More sharing options...
ps8modules Posted September 20, 2022 Share Posted September 20, 2022 Thank you. Please give me a moment to examine the module. Link to comment Share on other sites More sharing options...
SheldonCooper Posted September 20, 2022 Author Share Posted September 20, 2022 6 minutes ago, 4you.software said: Thank you. Please give me a moment to examine the module. Sure Link to comment Share on other sites More sharing options...
ps8modules Posted September 20, 2022 Share Posted September 20, 2022 (edited) Updated module. Uninstall the previous module. customproductname.zip Result: Edited September 20, 2022 by 4you.software (see edit history) 1 Link to comment Share on other sites More sharing options...
SheldonCooper Posted September 20, 2022 Author Share Posted September 20, 2022 50 minutes ago, 4you.software said: Updated module. Uninstall the previous module. customproductname.zip 3.92 kB · 0 downloads Result: It worked. Not have enough words to thank you. Thank you so much. Let me know if i can help you ever in anything.🤝 1 Link to comment Share on other sites More sharing options...
ps8modules Posted September 20, 2022 Share Posted September 20, 2022 I gladly helped 😉 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