Passing path arguments to a form builder I'm trying to understand how to port the following D7 code to Drupal 8.

function x_menu() {
    $menu['x/form/test/%/%'] = [
        'page callback' =&gt; 'drupal_get_form',
        'page arguments' =&gt; ['x_form_test', 3, 4],
        'type' =&gt; MENU_CALLBACK,
        'title' =&gt; 'Test Form',
    ];
    return $menu;
}


function x_form_test(&amp;$form, $form_state, $arg1, $arg2) {
    return $form;
}


From what I can glean from the documentation of the Routing and Form API, the new way to connect a path to a form is to put this in x.routing.yml:

x.form_test:
    path: 'x/form/{arg1}/{arg2}',
    defaults:
        _form: '\Drupal\x\XFormTest'
        _title: 'Test Form'


and then create a form class XFormTest that ultimately descends from FormBase.

However, it's clearly impossible* to add extra arguments to an inherited function signature like public FormInterface::buildForm(array $form, FormStateInterface $form_state)

How can I read $arg1 and $arg2 in XFormTest::buildForm(), preferably while still pointing the route at the form rather than putting a Controller around it?

(Routing API: https://www.drupal.org/node/2092643)

*Update: Maybe it's not so much impossible as that I'm not doing it correctly.

My function declaration

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class XBBCodeHandlerForm extends FormBase {
  public function buildForm(array $form, FormStateInterface &amp;$form_state, $format = XBBCODE_GLOBAL) {
  }
}


generates the following fatal error:

PHP Fatal error:  Declaration of Drupal\xbbcode\Form\XBBCodeHandlerForm::buildForm() 
must be compatible with 
Drupal\Core\Form\FormInterface::buildForm(array $form, 
Drupal\Core\Form\FormStateInterface $form_state)
 in /home/arancaytar/software/web/drupal/modules/xbbcode/src/Form/XBBCodeHandlerForm.php
on line 161


Edit: I misunderstood the fatal error; it wasn't caused by the extra argument but by changing the value argument $form_state to a reference argument. PHP does allow adding optional arguments to overriding functions, apparently.
it's clearly impossible to add extra arguments to an inherited function signature


Not true - adding extra arguments to a function signature is one of the fundamental reasons that inheritance exists in OO.

You literally just need to add your arguments to the function signature to make this work:

public function buildForm(array $form, FormStateInterface $form_state, $arg1, $arg2) {
    // You have $arg1 and $arg2 available
}


You could do something more fancy and get typed data by using parameter upcasting, otherwise you'll get the args as strings.

Examples here.