I'd like to insert the login form in my own form. To be clear, I have a quotation form and after we answer all the questions I would like to find the user login block. And after the user is logged in, I would like to reload the page to show differents elements.

In the form, I insert this code to show the login block:

$dgf_login_block = drupal_get_form('user_login_block');
$render_login_block = drupal_render($dgf_login_block);
$form['envoi']['login_block'] = array(
    '#markup' => $render_login_block ,
    '#prefix' => '<div class="lead text-left">',
    '#suffix' => '</div>',

But it doesn't work.

My question is: what is the best way to insert the user login form inside an other form? Or does it better to define two fields (username and password) and validate them (and how can I validate the login)?

Thanks in advance for your help.



In general it is not possible to have one form inside of another form. So even if you do it with pure HTML, you can't have another form inside of the form tag.

To have login block you will need to add it as a separate form. So either you can redirect user to a page where you have login form after they have filled your original form. I don't have reputation to comment but I would like to know how have you created your original form. That way I might be able to help you with what you want to achieve.

  • Thanks a lot Bharat for your answer and your help. I've created my form in a personalized module. It's a multistep form. My aim is to have at this step (the result of the quotation) two choices : if you're not logged in, you only see the price; if you're logged in, you can see details. So if the loggin form is in an another page, I think it's difficult to reload the second step of the form with the user answers. But I may be on a wrong track… Thanks again. – A.Lassaigne Sep 15 '17 at 13:00

I found a solution for a multistep form. I used the user login block, and in my module :

function mymodule_form_user_login_block_alter(&$form, &$form_state) {
  $form['field_data_hidden'] = array('#type'=> 'hidden');
  $form['#submit'][] = 'mymodule_user_redirect'; 
function mymodule_user_redirect($form, &$form_state) {
  drupal_goto('my-form-page-to-reload', array('query'=>array('data'=>$form_state['values']['field_data_hidden'])));

The hidden field stores the datas for the query from the multistep form (with JS). The query is used for reload the form with the previously entered data.

Note: if you have trouble for redirect the page in the module, take care that the "TRIGGER: AFTER A USER HAS LOGGED IN" has no assign.

There's may be a best way to do this, but it works.

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.