This is not asked in the question. Maybe it could also help, I had to pass a parameter to a form that was not loaded by a route. The form is integrated into a template loaded by a controller.
In custom_module.routing.yml
:
path: '/lorem/{arg}'
defaults:
_controller: '\Drupal\my_module\Controller\LoremController::testLorem'
In the controller, the form is called and the parameter $arg
is passed to the form builder.
public function testLorem($arg) {
$form = \Drupal::formBuilder()->getForm('Drupal\my_module\Form\testForm', $arg);
$build = [
'#theme' => 'custom_theme',
'#form' => $form,
'#arg' => $arg,
];
return $build;
}
And in the form :
public function buildForm(array $form, FormStateInterface $form_state, arg$arg = null) {
}