24

What is the alternative to drupal_get_form('user_login'). in Drupal 8?

I am using the following code in theme_name.theme, but it doesn't work.

// Include namespace for userLoginForm.
use \Drupal\Core\core\modules\user\src\Form;

function MYTHEMENAME_preprocess_page(&$variables) {
  $form = UserLoginForm::create();
} 

6 Answers 6

36

drupal_get_form() has been replaced by the form builder service, e.g.:

$form = \Drupal::formBuilder()->getForm(\Drupal\user\Form\UserLoginForm::class);

See the Change Record for more details.

Actually, don't - that change record is incorrect. It'll tell you to pass the form ID, when in fact you need to pass the class string.

0
15

You can create a form under form folder . And then you can call your form anywhere and pass the paramater as shown.

Drupal 7 : $form = drupal_get_form('form_id',$parameter);

Drupal 8 : $form = \Drupal::formBuilder()->getForm('Drupal\custom_module\Form\CustomModuleForm',$parameter);

2
  • 3
    How to get a node add form, for eg: add article form (/node/add/article) ?
    – JayKandari
    Aug 1, 2016 at 11:52
  • 2
    +1, you can pass it without $parameter in controller as well.
    – Mr.T
    Mar 30, 2017 at 21:10
8

There are two options to return(D8) or render(D7) a form. These are \Drupal::formBuilder()->getForm and Drupal::formBuilder()->buildForm. I use \Drupal::formBuilder()->getForm as below.

Use this if you are not passing a parameter:

$form = \Drupal::formBuilder()->getForm('\Drupal\example\Form\ExampleForm');
return $form;

Use this if you are passing a parameter:

$parameter = "your_parameter";
$form = \Drupal::formBuilder()->getForm('\Drupal\example\Form\ExampleForm', $parameter);
return $form;
//pass to formbuild function
public function buildForm(array $form, FormStateInterface $form_state, $parameter = NULL){//form code}


Drupal 7 Equivalent of the above:

$parameter = 'your_parameter';
$form = drupal_get_form('form_id', $parameter);
print drupal_render($form);
//pass to form function
function form_id ($form, &$form_state, $parameter){//form code}

Cheers. Let me know if it helps.

1
  • Gosh, it took me ages again, although you even cover this in your answer, that the additional parameter passed to buildForm must always be initiated with NULL.
    – leymannx
    Apr 29, 2020 at 16:55
1

The documentation does allow an instance of the class as well. So

$formObj = new \Drupal\demo\Form\MyForm();
$form = \Drupal::formBuilder()->getForm($formObj);

is also correct.

1
  • 2
    Probably don't do this. In case the form class uses dependency injection, you've circumvented it here. Better off to let the form builder instantiate the class unless you have a good reason to do so. Aug 17, 2018 at 19:39
1

Ok if all else fails here is a sneaky trick by setting a new form state object.

use Drupal\Core\Form\FormState;
...

$form_state = new FormState();
$form_state->set('name', $name);
$form_state->set('user', $user);
$render_array = \Drupal::formBuilder()->buildForm('\Drupal\mod\Form\MyForm', $form_state);
...
// Example render.
$response = new AjaxResponse();
// Show a dialog box.
$dialogText['#attached']['library'][] = 'core/drupal.dialog.ajax';
$response->addCommand(new OpenModalDialogCommand("Dialog Title", $render_array, ['width' => '300']));
return $response;

... then in your form method buildForm you can then...

 // Get form state.
 $name = $form_state->get('name');
 if (!empty($name)) {
    $form['name'] = [
      '#type' => 'item',
      '#markup' => $name,
    ];
  }
-1

Assuming the below is your form class

namespace Drupal\demo\Form;
use \Drupal\Core\Form\FormBase;
class MyForm extends FormBase {
}

You have to create a new instance from your form class first, and pass it to formBuilder's getForm.

$form_obj = new \Drupal\demo\Form\MyForm();
$form = \Drupal::formBuilder()->getForm($form_obj);
1
  • 2
    You don't "have to create a new instance" - this is wrong. You should be passing the class string.
    – Christian
    Jul 27, 2016 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.