[Drupal] How to Render Drupal Form in Node

December 13, 2017 - 14:14

To render a Drupal form to a node, drupal_build_form function is used. This function allows us to render all forms except the admin forms. Note that, admin forms didn't find success while rendering the same. In-order to render a form the following syntax is used,

$form_id = 'user_login_form';
  $form_state = array();
  $form_state['build_info']['args'] = array();
  $form_state['build_info']['files']['menu'] = array();
  $form = drupal_build_form($form_id, $form_state);
  return render($form);

Here we are trying to render user login form, if you are logged in as a user it will redirect to user profile page. Here form state values are built and are passed as parameter along with form id for the drupal_build_form function.

Instead of drupal_render(), simply render() is used to render. This will output the form in the node page. Hope this help, happy coding. For more assistance, contact us and for Drupal solutions, have a look on our services.

Get Drupal updates straight to your inbox

To prevent automated spam submissions leave this field empty.

Post your comments / questions