In a preprocess function we can use this code to render the default form.

$default_form = \Drupal::config('contact.settings')->get('default_form');
$entity = \Drupal::entityManager()->getStorage('contact_form')->load($default_form);

$message = \Drupal::entityManager()
   ->create(array('contact_form' => $entity->id(),

$form = \Drupal::service('entity.form_builder')->getForm($message);

How do we render another form? I have tried $my_form = \Drupal::config('contact.settings')->get('my_form_id'); without success.


A contact form?

your_form_id is the form, you don't have to load a setting for that. If you know what contact form you want to display, just replace $default_form with your form_id and it should work.

  • Thanks @Berdir ! Just one mention: I created a custom form with the machine name product_information. It seems that you sould pass the machine name of the bundle entity, rather than the form id like in hook_form_FORM_ID_alter. – Oana Hulpoi Jun 1 '16 at 12:39
  • The code you shared is about contact forms, which are config entities created in the UI. If product_information would be such a form, you'd just write $default_form = 'product_information'. If you have a custom form, then that's completely different. You just need something like this: \Drupal::formBuilder()->getForm('Drupal\user\Form\UserLoginForm'). Class name, not form id. – Berdir Jun 1 '16 at 16:40

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.