简体   繁体   English

Sylius ResourceBundle:如何覆盖表单服务?

[英]Sylius ResourceBundle: how to override a Form service?

In ResourceBundle, I've configured a resource "charge.quote" : 在ResourceBundle中,我配置了一个资源“ 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. 我想知道如何覆盖由ResourceBundle生成为charge.form.type.quote的表单服务。 The aim is to be able to access to the Service Container from QuoteType. 目的是能够从QuoteType访问服务容器。

I tried to declare an other service to override the service generated by ResourceBundle, like this: 我试图声明一个其他服务来覆盖ResourceBundle生成的服务,如下所示:

<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. 但是自定义窗体被忽略了:ResourceBundle从Entity生成通用窗体。

Any idea to pass the Service Container to my form class? 有将服务容器传递给表单类的想法吗?

Thanks ! 谢谢 !

Same issue was recently solved here: https://github.com/Sylius/Sylius/issues/3843 最近在这里解决了相同的问题: 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. 您只需定义一个具有相同名称的服务即可,例如app.form.type.book并为其指定别名app_book。 Sylius will use it from now. Sylius从现在开始将使用它。 :) :)

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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM