Jump to content

Override SRC file


SheldonCooper

Recommended Posts

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 by 4you.software (see edit history)
Link to comment
Share on other sites

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 by SheldonCooper (see edit history)
Link to comment
Share on other sites

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 by 4you.software (see edit history)
Link to comment
Share on other sites

<?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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...