New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix compatibility with Symfony 3.2 form renderer #4216
Conversation
@@ -105,7 +105,12 @@ public function listAction() | |||
$formView = $datagrid->getForm()->createView(); | |||
|
|||
// set the theme for the current Admin Form | |||
$this->get('twig')->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')->renderer->setTheme($formView, $this->admin->getFilterTheme()); | |||
if ($this->has('twig.form.renderer')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
has
does not seem to exist (see Travis)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->get('twig.form.renderer')->setTheme($formView, $this->admin->getFilterTheme()); | ||
} else { | ||
// BC for Symfony < 3.2 | ||
$this->get('twig')->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')->renderer->setTheme($formView, $this->admin->getFilterTheme()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please linebreak this
@@ -112,7 +112,13 @@ public function appendFormFieldElementAction(Request $request) | |||
|
|||
$extension = $this->twig->getExtension('Symfony\Bridge\Twig\Extension\FormExtension'); | |||
$extension->initRuntime($this->twig); | |||
$extension->renderer->setTheme($view, $admin->getFormTheme()); | |||
|
|||
if ($this->has('twig.form.renderer')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HelperController
does not extend the standard sf controller, hence the error you get in Travis
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can get the container from the pool if you wish (it's a very ugly, but should do the trick).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alternative would be to simply extend the standard controller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I will inject twig.form.renderer
service here inside constructor. Controller as a service helps a lot here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, but you will have do inject it conditionally, since it does not necessarily exist, right? my bad there is always a renderer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It exists since 2.3 (or even earlier but we support >= 2.3) https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml#L102 but it was private service before 3.2. Private services cann't be retrieved at runtime but they can properly injected into another services.
@@ -717,4 +717,14 @@ public function getValidatorInterfaces() | |||
|
|||
return $data; | |||
} | |||
|
|||
protected function getTwigRenderer() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private?
1fc343c
to
f5a7885
Compare
@@ -1364,4 +1363,18 @@ protected function preList(Request $request) | |||
|
|||
return $this->get('translator')->trans($id, $parameters, $domain, $locale); | |||
} | |||
|
|||
protected function setFormTheme(FormView $formView) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PHPdoc missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would prefer a private method, because we will move the twig stuff to the template completly.
*/ | ||
public function __construct(\Twig_Environment $twig, Pool $pool, AdminHelper $helper, $validator) | ||
public function __construct(\Twig_Environment $twig, Pool $pool, AdminHelper $helper, $validator, TwigRenderer $twigRenderer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a BC break. Please make the last parameter optional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And use TwigRendererInterface
as type hinting, it will be easier to make the build pass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@greg0ire it is deprecated since 3.2 https://github.com/symfony/twig-bridge/blob/master/Form/TwigRendererInterface.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh… too bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to stay BC here, this is a BC break!
d1a37d8
to
512b4d5
Compare
@@ -1348,6 +1347,23 @@ protected function preList(Request $request) | |||
{ | |||
} | |||
|
|||
protected function setFormTheme(FormView $formView) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- private please
- add phpdoc
@@ -497,4 +493,18 @@ private function retrieveFilterFieldDescription(AdminInterface $admin, $field) | |||
|
|||
return $fieldDescription; | |||
} | |||
|
|||
protected function setFormTheme(FormView $formView, $theme) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- private please
- add phpdoc
This is a Symfony BC break. I opened a pull request: symfony/symfony#20710. |
Just saw this PR. FYI: I have a similar approach, but use the container to retrieve the renderer, which should save some hassle. cedricziel@23707f9 |
@cedricziel I've tryed this before. The problem that we need pass another dependency to |
@Koc the |
@Koc are you stuck? If yes how can we help? Should we ask @cedricziel to make a PR? |
512b4d5
to
6ee4761
Compare
@greg0ire just need more time to fix tests |
6ee4761
to
513116b
Compare
* | ||
* @param FormView $formView | ||
*/ | ||
protected function setFormTheme(FormView $formView) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still need a private
method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method can be useful for custom actions that extends from crud controller and used forms. We have a lot setFormTheme
calls in our actions and for now they also not works anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, but we will remove the feature to set the form rendering in the controller. This will be done in the template files with the next major.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #4088
@@ -328,6 +329,20 @@ public function testappendFormFieldElementAction($validatorInterface) | |||
|
|||
$twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface')); | |||
$twig->addExtension(new FormExtension($mockRenderer)); | |||
|
|||
if (Kernel::MAJOR_VERSION >= 3 && Kernel::MINOR_VERSION >= 2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, if we could rely on the kernel version.
cc @greg0ire
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't. I'm currently waiting for the tests to pass, and then I think we should do a complete review, but yeah this is not the way to go. Detecting the presence of the public property might be a solution, or anything else that changed since 3.2 if it is too hard (the property does not exist at all times).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Why do you think it might be a bad idea to rely on Kernel
to bring the version information? Maybe I'm mistaken, but those fields provide exactly the info we need. I'd rather switch to Kernel::VERSION_ID >= 30200
though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is your answer right from the sf docs : https://symfony.com/doc/current/setup/bundles.html#updating-your-code-to-support-symfony-2-x-and-3-x-at-the-same-time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting-thank you for the link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're very welcome :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cedricziel the bundle depends on individual packages, not on the fullstack repo (to give flexibility to users), but this allows to have different versions of each package. Kernel constants give you the version of HttpKernel, not of the Twig bridge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not simply doing |
Please test symfony/symfony#20769 |
Triggering a new build. Let's pray ⛪ |
Let's not celebrate prematurely, but it looks good :) |
@Koc please fix the StyleCI build |
Aaaand it's green! Congrats! |
@greg0ire done |
Have someone tested it in real life application (not only unit tests)? does it really works? |
@wouterj maybe you can? I don't have such an app lying around. |
->method('getRuntime') | ||
->will($this->returnCallback(function ($name) use ($twigRenderer) { | ||
switch ($name) { | ||
case 'Symfony\Bridge\Twig\Form\TwigRenderer': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A switch with one case… 😕
Just test-drove it in a (rather minimal) sf 3.2 app. All nice and dandy so far. |
requested changes were addressed
@core23 @OskarStark please give this a final review and merge |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested this PR in the CMF test jobs and previously failing builds are now fixed. So I can confirm this fixes the bug.
@core23 @OskarStark this needs to be squash-merged (or at least, some of the commits should be squashed together) |
@sonata-project/contributors : this is kind of a blocker. I'll merge this afternoon if you don't have anything to say. |
Thanks @Koc ! And thanks everyone for testing / reviews ! |
3.10.1 has been released with this fix in it! |
Do you know, that this code
works fine in Symfony 3.2.1? |
@axzx but it triggers deprecation warnings |
…4216) Our code was relying on an undeclared private property of the form Twig extension to get a renderer object, in order to set the form theme. On recent versions of Symfony, the renderer is now obtained from the Twig environment.
I am targetting this branch, because this is bugfix.
Closes #4152, #4154
Changelog
Subject
Proposed sollution in #4154 breaks compatibility with older Symfony versions because
twig.form.renderer
service is private https://github.com/symfony/symfony/blob/2.8/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml#L156 . Also I've updated code in other places.