Skip to main content
improve code snippet
Source Link

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) {
}

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.

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);
}

And in the form :

public function buildForm(array $form, FormStateInterface   $form_state, arg = null) {
}

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 = null) {
}
added 122 characters in body
Source Link

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.

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);
}

And in the form :

public function buildForm(array $form, FormStateInterface   $form_state, arg = null) {
}

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.

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);
}

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.

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);
}

And in the form :

public function buildForm(array $form, FormStateInterface   $form_state, arg = null) {
}
Source Link

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.

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);
}