Skip to content

[Form] Added explicit getParent() call in types inheritance mechanism #13488

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

Closed
wants to merge 2 commits into from

Conversation

HeahDude
Copy link
Contributor

@HeahDude HeahDude commented Apr 4, 2020

No description provided.

@HeahDude HeahDude added the Form label Apr 4, 2020
@HeahDude HeahDude added this to the 3.4 milestone Apr 4, 2020
@HeahDude HeahDude requested review from wouterj and xabbuh April 4, 2020 23:52
@HeahDude HeahDude force-pushed the form/fix-get_parent branch from 1513ec3 to 526282e Compare April 5, 2020 00:03
@HeahDude HeahDude force-pushed the form/fix-get_parent branch from 526282e to 4fc04cc Compare April 5, 2020 19:41
@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 5, 2020

Thanks for the review, I've addressed everything.

@HeahDude HeahDude force-pushed the form/fix-get_parent branch from 4fc04cc to e573c0a Compare April 5, 2020 20:10
Copy link
Member

@wouterj wouterj left a comment

Choose a reason for hiding this comment

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

The only thing I'm not sure if I'm really missing is that you must have FormType or any of its children as parent. I think it's sort of "unnecessary" information, but I also can see someone creating a type that has a completely weird parent (does the code catch this nicely, e.g. if I return stdClass there?)

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 5, 2020

The only thing I'm not sure if I'm really missing is that you must have FormType or any of its children as parent. I think it's sort of "unnecessary" information

Basically, when creating child types, the intention should be one of the following:

  • extend the FormType
  • extend the ButtonType
  • extend any type (built-in or custom)

The AbstractType makes the first option the default.

but I also can see someone creating a type that has a completely weird parent (does the code catch this nicely, e.g. if I return stdClass there?)

Yes, getParent expected a class name implementing FormTypeInterface and is checked against circular reference.

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 5, 2020

I've edited my comment as I mixup up with type extensions.

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 5, 2020

Th fourth option would be to implement the FormTypeInterface directly (enforcing implementing getBlockPrefix and empty getParent), and without even extending the abstract BaseType (with extends as done by FormType and ButtonType), that would make the type unable to use default options and configuration.

Maybe we should remove that sentence and add a clear note to explain all that. WDYT?


.. _form-type-methods-explanation:

``getParent()``
Copy link
Member

Choose a reason for hiding this comment

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

This description focuses on "what/how" instead of "when/why". It explains that this is "like PHP inheritance but implemented in PHP, because you can't use real PHP inheritance".

But I'm missing information about the "why" and "when" to use this:

  • Is it mandatory to define this method?
  • Which value should return this method? (the FQCN of a class implementing FormType??)
  • How should I use this? (e.g. if you create a form type similar to an existing one, extend from it ... otherwise, extend from the generic FormType ??)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've pushed a rewording.


.. _form-type-methods-explanation:

``getParent()``
This return the class name of the type configuring the form before. It
Copy link
Member

Choose a reason for hiding this comment

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

returns

Copy link
Member

Choose a reason for hiding this comment

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

I am not really sure if I completely understand what the meaning of the "the form before" part is supposed to be.

means all the other methods below will be called with this parent type
and all its type extensions before calling the ones defined in your
custom type.
By default, all classes inherit the ``AbstractType::getParent()`` which
Copy link
Member

Choose a reason for hiding this comment

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

"[...] all classes extend the AbstractType class [...]" or "[...] all classes inherit the AbstractType::getParent() method [...]"?

and all its type extensions before calling the ones defined in your
custom type.
By default, all classes inherit the ``AbstractType::getParent()`` which
defines the ``FormType`` as parent type to re-use its options and
Copy link
Member

Choose a reason for hiding this comment

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

[...] as the parent type [...]

``configureOptions()``
This defines options for your type that
can be used in ``buildForm()`` and ``buildView()``. Options are inherited
from parent types and parent types extension but you can create any custom
Copy link
Member

Choose a reason for hiding this comment

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

[...] and parent type extensions, but [...]

but you can create any others that you need here.
``finishView``
Same as ``buildView`` but to configure the nested field views.
This method as no use with types that build compound forms.
Copy link
Member

Choose a reason for hiding this comment

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

This method has no use [...]

Copy link
Member

Choose a reason for hiding this comment

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

This method as no use with types that build compound forms.

By the way, isn't that the other way around?

Also, if you need to modify the "view" of any of your child types from
your parent type, use the ``finishView()`` method.
If you're creating a field that consists of nested fields, then be sure to
set the parent type as ``FormType::class`` or one of its child.
Copy link
Member

Choose a reason for hiding this comment

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

[...] or one of its children.

@wouterj
Copy link
Member

wouterj commented Oct 21, 2020

Hi @HeahDude. Do you have any time to finish on this PR?

@wouterj
Copy link
Member

wouterj commented Nov 21, 2020

Closing in favor of #14588

@wouterj wouterj closed this Nov 21, 2020
javiereguiluz added a commit that referenced this pull request Nov 23, 2020
…tance mechanism (HeahDude, wouterj)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] Added explicit `getParent()` call in types inheritance mechanism

Continues #13488

Most of the original PR was already fixed in a slight rewrite of this article, but I think adding `getParent()` docs is still important.

Commits
-------

97c94d7 Updated getParent() description with the why/when (thanks javier!)
68813a0 [Form] Added explicit `getParent()` call in types inheritance mechanism
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.

5 participants