You are asking two questions here. I've tried to answer both in general terms for all forms, and provided specifics for entity forms which are a bit special.
1. How do I handle options parameters in routes?
This is fairly simple, you can provide a default value for the route, say you had a routing file looking something like this
some.route:
path: '/foo/{bar}/{baz}'
defaults:
baz: 42
...
using defaults
you can specify default values for the parameters. You can specify as many default values as you like, and Drupal will catch it, when there is no more unknown parameters. In the above example you would need to specify a value for bar
so
foo/some-value
foo/some-value/42
would both give same result
2. How do I add additional parameters to en (entity) form?
Solution a
There are two ways to do this. You can add additional parameters in getForm
on the form builder which will be available in the ::buildForm
method. The EntityFormBuilder
is a bit special in that it already has some support for this, the 3 argument to get form is array with data, which will be added to FormState which is used for ::buildForm
and ::form
In code (for entity form class) it looks like this
$form = \Drupal::service('entity.form_builder')->getForm($entity, 'default', ['bar' => 'some_data']);
and in the entity form class
public function form(array $form, FormStateInterface $form_state) {
$storage = $form_state->getStorage();
$storage['bar'] == 'some_data';
}
Solution b
The other method is often a bit more complex. Instead of using the entity form build, you can instantiate the form class and add whatever you want in the ::__construct
. Entity forms are are bit special as they don't have constructors, instead they have setters and a method to get the form class.
In code you entity forms, it could look like this
$form_object = \Drupal::entityTypeManager->getFormObject($entity_id, $operation);
$form_object->setEntity($entity);
$form_object->setCustomValue($value);
$form_render = \Drupal::formBuilder->getForm($form_object);
The advantage with this latter method, is that the data you add to the form class, will be available in all method calls. One issue could be that you need to display multiple variations of the same form on the same page. For Drupal to do this, they need a unique form id, which could be handled in the getFormId
method. Or maybe you need to use a service, where it would be messy to use the form building method to store it some where and pass it through the submission process.