Search code examples
symfonysymfony-3.3

Symfony 3.3 - How can the container know the tag of the service when autowiring is enabled?


I have a service which has two tags, extended_type and name.

With autowiring enabled, how can I define these tags?

Edit: This is my class form extension

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
class DateTimeTypeExtension extends AbstractTypeExtension
{
    public function getExtendedType()
    {
        return DateTimeType::class;
    }

    public function buildView( FormView $view, FormInterface $form, array $options )
    {
        $view->vars['date_time_help'] = 'Format d-m-Y.';
    }

}

Form type extension service need to define two tags extended_type and name


Solution

  • Starting in Symfony 3.3, if you enable autoconfigure, then some tags are automatically applied for you like twig.extension. enable autoconfigure does not work for all tags. Many tags have required attributes, like event listeners, where you also need to specify the event name and method in your tag. Autoconfigure works only for tags without any required tag attributes

    In your case, you need to override your sevice in the app/config/service.yml and explicitly define your tags like this:

    AppBundle\Service\YourService:
            tags:
                - { name: service1, extended_type: service2 }