Skip to content

[Form] add a convenience method to get the parent form in Twig templates #28812

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
Feb 13, 2019

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Oct 11, 2018

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

@ro0NL
Copy link
Contributor

ro0NL commented Oct 11, 2018

it's a BC break no? also ref #19492 (comment)

@xabbuh
Copy link
Member Author

xabbuh commented Oct 11, 2018

Why would that be a BC break?

@xabbuh
Copy link
Member Author

xabbuh commented Oct 11, 2018

Taking into account #19492 (comment) we could of course think about adding a Twig function instead if we do not want to add this method to the FormView class.

@ro0NL
Copy link
Contributor

ro0NL commented Oct 11, 2018

the order changes right? before a child named parent might be returned first, whereas now it's always the parent form

@ro0NL
Copy link
Contributor

ro0NL commented Oct 11, 2018

alternatively, getting rid of arrayaccess in formview would also do it towards consistent accessors in php as well as twig, but that's a bigger step :)

@xabbuh
Copy link
Member Author

xabbuh commented Oct 11, 2018

If you refer to form.parent, this still returns the child form as Twig considers ArrayAccess methods before looking at actually implemented methods. And form.getParent() just is not usable without this change.

alternatively, getting rid of arrayaccess in formview would also do it towards consistent accessors in php as well as twig, but that's a bigger step :)

I don't think that's a good idea DX wise. Being able to use form.child_name is quite convenient.

@yceruto
Copy link
Member

yceruto commented Oct 11, 2018

@xabbuh could we deprecating twig_is_root_form() test func then? I guess it wouldn't useful anymore, replacing it by {% form.getParent() is null %}.


In the other hand, I think the solution should be done in Twig context, because having a getter for a public property seems odd.

What about adding a new Twig function for access to the FormView public properties? Something like this:

public function getPropertyValue($object, string $propertyName)
{
        return $object->$propertyName;
}

for cases where the public property matches the name of the child form (ArrayAccess ambiguity in Twig context):

{# $formView->parent #}
{% if property_value(form, 'parent') is null %}

{# $formView->vars #}
{% if property_value(form, 'vars').attr.class is defined %}

even, other ArrayAccess objects (in userland) with a composition similar to FormView can benefit from it, too.

@xabbuh
Copy link
Member Author

xabbuh commented Oct 31, 2018

I have rewritten this solution to add a new parent_form() Twig function instead. I would not deprecate the filter for now. Both the filter and the new function serve their own use cases.

@javiereguiluz
Copy link
Member

@xabbuh please, create an issue in Symfony Docs to not forget about this. Also, please add some usage examples and if this improves an existing feature add a before/after example. Finally, please check if this fixes something that the current docs say it's not possible to do or buggy. Thanks!

@fabpot
Copy link
Member

fabpot commented Feb 13, 2019

Thank you @xabbuh.

@fabpot fabpot merged commit cb60642 into symfony:master Feb 13, 2019
fabpot added a commit that referenced this pull request Feb 13, 2019
… in Twig templates (xabbuh)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Form] add a convenience method to get the parent form in Twig templates

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

Commits
-------

cb60642 add a convenience method to get the parent form in Twig templates
@xabbuh xabbuh deleted the issue-28686 branch February 13, 2019 09:41
@xabbuh
Copy link
Member Author

xabbuh commented Feb 16, 2019

@javiereguiluz it took me some time, but here we go now: symfony/symfony-docs#10999

javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Feb 16, 2019
This PR was merged into the master branch.

Discussion
----------

document the parent_form() Twig function

documents symfony/symfony#28812

Commits
-------

2ea9797 document the parent_form() Twig function
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
@fabpot fabpot mentioned this pull request May 9, 2019
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.

7 participants