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.