将表单扩展名应用于某些Symfony表单,而不是其他表单

I am designing a Symfony bundle that has two distinct types of forms:

  • Some "regular" forms
  • Some forms that are used exclusively in a REST API

For the latter group, I need to inject some services and apply some customization. I'd prefer to do this without declaring each form as its own service.

My thought was to do this:

  • Register a new form type that extends "form" with its own name like "api"
  • Extend that new "api" type for each form I wish to create
  • Create a form extension that is tagged to this "api" type
  • Within the extension, inject the services, apply transformation, etc.

The goal here is to make it extremely simple to bake in this extended functionality to new forms, for example by extending a new type or implementing a marker interface.

I was able to make most of the above work with one exception: I could only do it if I tagged the extension to "form", which then applied it to all forms. I haven't been able to get an extension to only target a subset of forms.

Is there any way to do this, or will I have to live with a less elegant solution?