Jump to content

Trouble overriding a Symfony service in PrestaShop 1.7


Recommended Posts

Hello there,

I'm writing a module that needs to override the ProductVirtual class (src/PrestaShopBundle/Form/Admin/Product/ProductVirtual.php).

As it is apparently a Symfony service, I'm trying to override it as instructed in the documentation.

To do that, I copied the original ProductVirtual.php file to modules/mymodule/ProductVirtual.php and replaced the namespace by MyModule and created the file modules/mymodule/config/services.yml with the following content:

services:
    form.type.product.virtual:
        class: MyModule\ProductVirtual
        arguments:
            - "@translator"
            - "@prestashop.adapter.legacy.context"
        parent: 'form.type.common_type'
        public: true
        tags:
            - { name: form.type }

However, what I get is a 500 error and the following exception:

Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Type error: Too few arguments to function PrestaShopBundle\Form\Admin\Product\ProductVirtual::__construct(), 0 passed in /bitnami/prestashop/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php on line 92 and exactly 2 expected" at /bitnami/prestashop/src/PrestaShopBundle/Form/Admin/Product/ProductVirtual.php line 52

Of course, 2 arguments are expected by the ProductVirtual's constructor but this FormRegistry.getType() method calls the constructor with no argument. I can't find a way to correct the problem and I have no knowledge in Symfony. I really need some advice here.

Thanks! :)

Link to comment
Share on other sites

  • 2 weeks later...
  • 5 years later...

Hello,

It was a looong time ago, but did you find a solution? I have the exact same problem but can't figure out what to do... Trying to override the service but in the end looks like it's still the old class that's called, as I can also see in your error message.

Link to comment
Share on other sites

Hello,

I found a workaround for this. Actually I misunderstood the idea of decorating a service, which is the way to go: https://devdocs.prestashop-project.org/8/modules/concepts/services/#decorate-the-service

The only (and not little) issue is that the doc is incomplete, in config/services.yml, it's missing the argument's part, here's what should be in the doc:

mymodule.my_own_b2b_feature_service: 
	class: 'YourCompany\YourModule\YourService' 
	decorates: 'prestashop.core.b2b.b2b_feature'
	arguments:
		- '@mymodule.my_own_b2b_feature_service.inner'

 

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