Drupal 9 image field migration

The problem is to add multiple times the same form on the page and process them. You can try to do it, but submitForm will be initiated for all forms, since they have the same id.
By default you can try to add the form in this way, but for builder form_id will be already present.

$form['multiple_form'] = \Drupal::formBuilder()->getForm('Drupal\my_module\Form\MyForm');

The idea is to create custom method, to which we'll pass an ID. And edit getFormId() method. The lines below should be added to your Form class.

private $custom_id;
public function getFormId(){
  return 'my_module_form_' . $this->custom_id;
public function setCustomId($id) {
  $this->custom_id = $id;

And now we can get instance of our Form class and add the ID in loop.

$formObject = \Drupal::service('class_resolver')->getInstanceFromDefinition('Drupal\my_module\Form\MyForm');

$formRenderable = \Drupal::formBuilder()->getForm($formObject);

$form['multiple_form'] = $form_renderable;

Add new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed.
       .o                       ooo        ooooo              oooooooooo.      oooooooo 
.d88 `88. .888' `888' `Y8b dP"""""""
.d'888 ooo. .oo. .oo. 888b d'888 oooooooo 888 888 d88888b.
.d' 888 `888P"Y88bP"Y88b 8 Y88. .P 888 d'""7d8P 888 888 `Y88b
88ooo888oo 888 888 888 8 `888' 888 .d8P' 888 888 ]88
888 888 888 888 8 Y 888 .d8P' .P 888 d88' o. .88P
o888o o888o o888o o888o o8o o888o d8888888P o888bood8P' `8bd88P'

Enter the code depicted in ASCII art style.