With Symfony2 I can embed a collection of form inside an another form (subform inside form). http://symfony.com/doc/current/cookbook/form/form_collections.html

I would like know if it's possible inside Drupal 8 please, The documentation (Drupal 8 - Form API) don't talk about that.


1 Answer 1


Drupal forms are very different from Symfony forms.

There is no concept of sub-forms, but there is no limit to the complexity a single form can have as a form is an almost arbitrary render array structure.

A commonly used concept there is #tree. By default, the form input element names are flat.

If want to edit multiple elements in the same form, you can build a structure like this:

$form['#tree'] => TRUE;
foreach ($things as $id => $thing) {
 $form['things'][$id]['name'] = ['#type' => 'textfield', ...]

Without #tree, you would have multiple textfields named 'name', when set to TRUE, you get something like name="things[5][name]".

In Drupal 7 and earlier, it was complicated to render those nested forms in a table, you needed custom theme functions that then built the actual table. In Drupal 8, it just works, in the above example, you would just need to add something like $form['things']['#type'] = 'table' and then each $id will be a table row.

Then you can get all those values in the submit method with $form_state->getValue('things') and process them again in a loop.

  • Nice, thank you! And it's possible to add more fields from a button with tree activated :) Dec 16, 2014 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.