0

I'm upgrading a project from 3.1 to latest 3.4 (3.4.15 at this time).

I've managed to upgrade all components and now I'm dealing with deprecations. In one controller, I have this function:

protected function themeForm(FormInterface $form)
{
    $themeFormView = $form->createView();
    $this
    ->get('twig.form.renderer')
    ->getExtension(FormExtension::class)
    ->renderer
    ->setTheme($themeFormView, '@admin/form/form.html.twig');
    return $themeFormView;
}

I've read the upgrade doc and I see that to remove the deprecation warning message, I have to use Symfony\Component\Form\FormRenderer instead of twig.form.renderer. However, the use case the example shows is different and I don't know how to remove the deprecation warning. I've searched for an alias, but I haven't found any. I've created an alias, but I get an error when trying to instantiate the class. As a result, I'd like to remove this warning. Can anybody help me with this? Thanks in advance.

The solution is to create a constructor:

protected $twig;

public function __construct(FormRenderer $twig)
{
    $this->twig = $twig;
}

and call the property later:

    $this->twig
    ->setTheme($themeFormView, '@admin/form/form.html.twig');

With this, I still can see the warning, so I have created a public alias in services.yml:

Symfony\Component\Form\FormRenderer:
    alias: 'twig.form.renderer'
    public: true

I can't see the deprecation warning now.

  • https://symfony.com/doc/3.4/form/form_customization.html - maybe it is time to switch to this way of theming? No need to write php code. – Dominic Wehrmann Sep 30 '18 at 01:55
  • What is the warning you get exactly? The `twig.form.renderer` service is still valid, only the class behind it has changed from `TwigRenderer` to `FormRenderer`, that does not seem related to your issue as you are not relying on any class but the service id directly. – chalasr Sep 30 '18 at 14:39
  • The exact warning is: User Deprecated: The "twig.form.renderer" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead. – Oscar Manuel Gómez Senovilla Sep 30 '18 at 22:22

1 Answers1

0

You should use

$this
    ->get('twig.form.renderer')
    ->setTheme($themeFormView, '@admin/form/form.html.twig')
;

instead of

$this
    ->get('twig.form.renderer')
    ->getExtension(FormExtension::class)
    ->renderer
    ->setTheme($themeFormView, '@admin/form/form.html.twig')
;

There was a circular reference, see https://github.com/symfony/symfony/pull/20093 and https://github.com/symfony/symfony/pull/20769 for references.

EDIT About your current issue: In short, using the dependency-injection container as a service locator is a bad thing, it is not meant to be. You should use true dependency-injection i.e. register your controller as a service and configure it so that the DIC gives it the services it needs, either through its constructor or via method (actions) arguments. Here is a perfect answer to a similar question that shows you how to do that: stackoverflow.com/questions/47743626/symfony-3-4-logger-service.

chalasr
  • 12,971
  • 4
  • 40
  • 82
  • Thanks. I got to the point you mention, but I still get the deprecation warning shown above. – Oscar Manuel Gómez Senovilla Sep 30 '18 at 22:26
  • Indeed another problem (but my answer should help you as well for another notice, `the ->renderer` public property has been removed in 4.0. About your current issue: In short, using the dependency-injection container as a service locator is a bad thing, it is not meant to be. You should use true dependency-injection i.e. register your controller as a service and configure it so that the DIC gives it the services it needs, either through its constructor or via method (actions) arguments. – chalasr Oct 01 '18 at 08:28
  • Here is a perfect answer to a similar question that shows you how to do that: https://stackoverflow.com/questions/47743626/symfony-3-4-logger-service. – chalasr Oct 01 '18 at 08:29