Register specific form type in Syfmony - Compiler Pass

Viewed 169

I have created a specific formType: OrganizationsType. This type takes 3 parameters in the constructor. The tokenStorage (TokenStorageInterface), the Router(Router) and an interface object from my class (PreparatorInterface). This form is inside a bundle and I want everyone to create his own Preparator by implementing PreparatorInterface.

So I wanted to create a Compiler pass that could register this FormType with all parameters. I try this :

$organizationListPreparator = $container->findTaggedServiceIds(self::TAG);

    if (empty($organizationListPreparator)) {
        throw new \Exception('CoffreoProOrganizationSelectorBundle need a preparator. Check README.');
    }

$container->register(OrganizationsForm::class, OrganizationsForm::class)
        ->addArgument(new Reference('security.token_storage'))
        ->addArgument(new Reference(key($organizationListPreparator)))
        ->addArgument(new Reference('router'))
        ->setAutoconfigured(true)
        ->setAutowired(true)
        ->setAbstract(true)
        ->addTag('form.type');

But when I try to instantiate a form like that

$organizationsForm = $this->createForm(OrganizationsForm::class);

How can i do something like that?

Edit

I receive this error message :

Too few arguments to function OrganizationSelectorBundle\Form\OrganizationsForm::__construct(), 0 passed in /var/www/myProject/vendor/symfony/form/FormRegistry.php on line 92 and exactly 3 expected

[2018-12-18 16:34:38] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Too few arguments to function OrganizationSelectorBundle\Form\OrganizationsForm::__construct(), 0 passed in /var/www/myProject/vendor/symfony/form/FormRegistry.php on line 92 and exactly 3 expected" at /var/www/myProject/vendor/organization-selector-bundle/Form/OrganizationsForm.php line 58 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Too few arguments to function OrganizationSelectorBundle\\Form\\OrganizationsForm::__construct(), 0 passed in /var/www/myProject/vendor/symfony/form/FormRegistry.php on line 92 and exactly 3 expected at /var/www/myProject/vendor/organization-selector-bundle/Form/OrganizationsForm.php:58)"} []

Edit

When i launch bin/console deb:container OrganizationForm

Information for Service "OrganizationSelectorBundle\Form\OrganizationsForm"


Option Value


Service ID OrganizationSelectorBundle\Form\OrganizationsForm
Class OrganizationSelectorBundle\Form\OrganizationsForm
Tags form.type
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired yes
Autoconfigured yes


I pass in my compiler pass when i add dump('Did I pass here'); die();

When i launch bin/console debug:container --tag form.type to list all services with tag form.type

Symfony Container Services Tagged with "form.type" Tag
======================================================

 --------------------------------------------------------------- ------- --------------------------------------------------------------- 
  Service ID                                                      alias   Class name                                                     
 --------------------------------------------------------------- ------- --------------------------------------------------------------- 
  App\Form\StaffingCustomerType     App\Form\StaffingCustomerType                                  
  App\Form\UserType                 App\Form\UserType                                              
 OrganizationSelectorBundle\Form\OrganizationsForm      OrganizationSelectorBundle\Form\OrganizationsForm           
 --------------------------------------------------------------- ------- --------------------------------------------------------------- 
1 Answers

I found a better solution than use CompilerPass.

I use directly a service definition in yaml and pass as second argument an specific service if. Then i provide a default service for this service id by using alias:

OrganizationSelectorBundle\Form\OrganizationsForm:
    arguments:
      - '@security.token_storage'
      - '@coffreo_organization_bundle.organization_list_preparator'
      - '@router'
    tags: [form.type]
    public: false

coffreo_organization_bundle.organization_list_preparator:
    class: Coffreo\Pro\OrganizationSelectorBundle\Preparator\AllOrganizationListPreparator
    arguments: ['@translator']
    public: false
Related