19

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

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.

  • thanks for your reply but i have already used it as you write and also i know drupal_get_form change to form builder but i want to know how to use it in my theme preprocess hook. – Kamal Oberoi Nov 30 '15 at 11:57
  • 1
    I'm not sure what you mean, unless $variables['foo_form'] = \Drupal::formBuilder()->getForm('user_login'); is it? – Clive Nov 30 '15 at 12:09
  • 2
    The argument is a class, not a form_id. Something like UserLoginForm::class. – Berdir Nov 30 '15 at 13:01
  • 1
    I recommend you use the PHP 5.5 ::class. It's a lot easier (use works) and avoids annoying typo's – Berdir Nov 30 '15 at 13:20
  • 1
    Please ask a new question for an unrelated topic @KamalOberoi - it'll provide more help to future visitors that way. But as a general rule, look in your server's error logs and the watchdog when you see that message – Clive Nov 30 '15 at 13:42
14

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

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

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.

2

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.

  • 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. – aaronbauman Aug 17 '18 at 19:39
0

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);
  • 2
    You don't "have to create a new instance" - this is wrong. You should be passing the class string. – Christian Jul 27 '16 at 7:23
0

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,
    ];
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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