Search code examples
phpsymfonyresourcebundlesylius

Sylius ResourceBundle: how to override a Form service?


In ResourceBundle, I've configured a resource "charge.quote" :

sylius_resource:
    resources:
        charge.quote:
            classes:
                controller: ChargeBundle\Controller\QuoteController
                model: ChargeBundle\Entity\Quote
                repository: ChargeBundle\Repository\QuoteRepository
                form:
                    default: ChargeBundle\Form\QuoteType

I'd like to know how I can override the form service, generated as charge.form.type.quote by ResourceBundle. The aim is to be able to access to the Service Container from QuoteType.

I tried to declare an other service to override the service generated by ResourceBundle, like this:

<service id="charge.form.type.quote" class="%charge.form.type.quote.class%">
    <argument type="service" id="service_container" />
    <tag name="form.type" />
</service>

But the custom Form become ignored: ResourceBundle generate a generic form, from the Entity.

Any idea to pass the Service Container to my form class?

Thanks !


Solution

  • Same issue was recently solved here: https://github.com/Sylius/Sylius/issues/3843

    In short answers taken out from the issue:

    You just define a service with same name, so app.form.type.book for example and give it an alias app_book. Sylius will use it from now. :)

    sylius_resource:
        resources:
            zdg.registration:
                templates: EGBundle:Backend/Registration
                classes:
                    model: ZDG\EGBundle\Entity\Registration
    

    Services:

    services:
        zdg.eg.type.registration:
            class: ZDG\EGBundle\Form\Type\RegistrationType
            tags:
                - { name: form.type, alias: zdg_registration }
            arguments: [@doctrine, @sylius.context.locale]
    

    Your form type name must match the alias. add to your Type:

    public function getName()
    {
        return 'zdg_registration';
    }