Skip to content

[Form] [TwigBridge] Added option to disable usage of default themes when rendering a form #22610

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

Merged
merged 1 commit into from
Oct 13, 2017

Conversation

emodric
Copy link
Contributor

@emodric emodric commented May 2, 2017

Q A
Branch? 3.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR symfony/symfony-docs#8495

This adds a possibility to use only keyword in form_theme tag to disable usage of globally defined form themes, e.g.

{% form_theme form with ['common.html.twig', 'form/fields.html.twig'] only %}

Otherwise, in order to completely control the rendering of the forms (for example in custom admin interfaces), one would need to use a form theme which has all the possible twig blocks defined to prevent globally defined themes to interfere with the rendering.

only keyword is already used when including a Twig template to transfer only the variables which are explicitly defined in the include tag, so it seemed natural to use it here too.

This, of course, means that the user will need to manually use all of the templates that are required to render the form, including form_div_layout.html.twig

This issue is described in details over at Symfony Demo repo: symfony/demo#515

TODO:

  • submit changes to the documentation

@@ -16,9 +16,9 @@
*/
class FormThemeNode extends \Twig_Node
{
public function __construct(\Twig_Node $form, \Twig_Node $resources, $lineno, $tag = null)
public function __construct(\Twig_Node $form, \Twig_Node $resources, $only, $lineno, $tag = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be added as last argument with default false to keep BC.

Copy link
Contributor

@sstok sstok Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this class considered an internal detail?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it's not marked as is, and anyone could extend the parser and the node to use custom implementation.

I don't know if it's a a doable/valid use case, but this API is public for now anyway, so BC should be preserved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HeahDude Will do! As I am currently on vacation, give me couple of weeks and I'll do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HeahDude Placed the $only argument at the end and rebased on top of 3.4.

@emodric
Copy link
Contributor Author

emodric commented Sep 25, 2017

Hi, what is the status PR? Do you think it could end up in 3.4?

@emodric emodric force-pushed the form_theme_only branch 2 times, most recently from 731de67 to 627eeec Compare September 25, 2017 12:17
*/
public function setTheme(FormView $view, $themes);
public function setTheme(FormView $view, $themes, $useDefaultThemes = true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding the argument in the interface is a BC break. So this cannot be done before 4.0 (and 3.4 should trigger a deprecation warning in case it finds an implementation of the interface without the extra optional attribute)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, makes sense. How should I proceed then?

*/
public function setTheme(FormView $view, $themes);
public function setTheme(FormView $view, $themes, $useDefaultThemes = true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@fabpot
Copy link
Member

fabpot commented Oct 1, 2017

@emodric I like this feature, can you implement the suggestions made by @stof?

@emodric
Copy link
Contributor Author

emodric commented Oct 1, 2017

@fabpot Sure, but how do you want me to handle it?

Open a new PR for deprecations only against 3.4 and target this one for master or did you have something else in mind?

@fabpot
Copy link
Member

fabpot commented Oct 1, 2017

You need to remove the argument and use get_func_args() to determine how it was called to handle the new arg and trigger a dep notice if needed. You can find quite a few examples in the code base doing exactly this.

@emodric
Copy link
Contributor Author

emodric commented Oct 1, 2017

Ah okay, I figured you wanted the feature to go in only in 4.0. I'll do it first thing tomorrow morning.

@emodric
Copy link
Contributor Author

emodric commented Oct 2, 2017

@fabpot I've updated the code to include requested deprecations. I updated the tests too in order not to trigger deprecation notices in them. There is a test failure in Twig bridge tests, but I don't see how it is related to this change.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(failure unrelated)

$this->renderer->setTheme($view, $themes);
$args = func_get_args();
if (!isset($args[2])) {
@trigger_error(sprintf('Calling setTheme method of %s without the third boolean argument $useDefaultThemes is deprecated since 3.4 and will be removed in 4.0. Call the method with the third argument included and use boolean true to keep current behaviour.', self::class), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we would want to force people to pass a third argument, which should be true by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. I'm not sure what exactly deprecation message should be then if the third argument is optional? Can't we just use it if it's there, and if not, default to true?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the right approach.

Copy link
Contributor Author

@emodric emodric Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, will fix it ASAP. Where do you want me to document the fact that in 4.0 the interface is going to receive a third argument? I've seen somewhere in the codebase the usage of an inline comment in the style of public function setTheme(FormView $view, $themes /* , $useDefaultThemes = true */ );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what we are doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot Alright, I've updated the code now as per instructions.

@emodric emodric force-pushed the form_theme_only branch 3 times, most recently from 3bed88b to 1d89877 Compare October 13, 2017 13:43
*/
public function setTheme(FormView $view, $themes);
public function setTheme(FormView $view, $themes /*, $useDefaultThemes = true */);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in all places where this is added, the code should throw a deprecation when the argument is not provided (using func_num_args, see other places in the code base).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas As per @fabpot, deprecation notice is not needed if the argument is optional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current code is good as is :)

@fabpot
Copy link
Member

fabpot commented Oct 13, 2017

Thank you @emodric.

@fabpot fabpot merged commit e0681f9 into symfony:3.4 Oct 13, 2017
fabpot added a commit that referenced this pull request Oct 13, 2017
…efault themes when rendering a form (emodric)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] [TwigBridge] Added option to disable usage of default themes when rendering a form

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | symfony/symfony-docs#8495

This adds a possibility to use `only` keyword in `form_theme` tag to disable usage of globally defined form themes, e.g.

`{% form_theme form with ['common.html.twig', 'form/fields.html.twig'] only %}`

Otherwise, in order to completely control the rendering of the forms (for example in custom admin interfaces), one would need to use a form theme which has all the possible twig blocks defined to prevent globally defined themes to interfere with the rendering.

`only` keyword is already used when including a Twig template to transfer only the variables which are explicitly defined in the `include` tag, so it seemed natural to use it here too.

This, of course, means that the user will need to manually `use` all of the templates that are required to render the form, including `form_div_layout.html.twig`

This issue is described in details over at Symfony Demo repo: symfony/demo#515

TODO:

- [x] submit changes to the documentation

Commits
-------

e0681f9 [Form] [TwigBridge] Added option to disable usage of default themes when rendering a form
@emodric
Copy link
Contributor Author

emodric commented Oct 13, 2017

Thanks everyone for review :)

This was referenced Oct 18, 2017
fabpot added a commit that referenced this pull request Oct 19, 2017
…dric)

This PR was merged into the 4.0-dev branch.

Discussion
----------

[Form] Add $useDefaultThemes flag to the interfaces

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

Followup to #22610 to add `$useDefaultThemes` to the interfaces as discussed in the issue.

Commits
-------

c22d783 [Form] Add useDefaultThemes flag to the interfaces
weaverryan added a commit to symfony/symfony-docs that referenced this pull request Nov 10, 2017
… form themes (emodric, javiereguiluz)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] Document disabling the usage of globally defined form themes

This PR documents the option to disable the usage of globally defined form themes. This is suggested as a feature in symfony/symfony#22610 (reviewed, but not yet merged)

Commits
-------

14ef7ae Minor reword
8d45f83 Document disabling the usage of globally defined form themes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants