0

I want to allow users to fill and submit a form even if they are not logged in, however if they are not authenticated yet, they should be redirected to login first, and after a successful login the submitted for will be really processed.

I will try to explain the solution that I did with a simple example:

function mymodule_menu() {
  $menu_items['custom/path/xyz'] = array(
    'page callback'   => 'mymodule_page_callback',
    [...]
  );
  [...]
}

function mymodule_page_callback() {
  // If there is a form_id_state saved in session, then
  // this is a redirect after login. Call the submit function again.
  if (isset($_SESSION['mymodule']['form_id_state'])) {
    $form_state = $_SESSION['mymodule']['form_id_state'];
    unset($_SESSION['mymodule']['form_id_state']);
    //drupal_form_submit('mymodule_form_id', $form_state);
    mymodule_form_id_submit(NULL, $form_state);
  }

  // Do normal things here.
  [...]

  return drupal_get_form('mymodule_form_id');
}

function mymodule_form_id_submit($form, &$form_state) {
  $op = $form_state['values']['op'];
  if ($op == t('Save') and !user_is_logged_in()) {
    // Save $form_state to session and redirect to login.
    $_SESSION['mymodule']['form_id_state'] = $form_state;
    drupal_goto('user', array('query' => drupal_get_destination()));
  }

  // Do normal form processing here.
  [...]
}

So, if user clicked Submit but he is not logged in, he is redirected to login with drupal_goto() and with come-back instructions, but first the state of the form is saved in the session.

Then, on page display, we check first whether there is a form state saved in session. If yes, then call again the form submit function, so that the processing of the form is finished (this time the user will be logged in).

All this seems a bit hackish to me. I wonder:

  • Are there any drawbacks to this approach?
  • Which is the best way to submit the form again,
    • calling the drupal_form_submit('mymodule_form_id', $form_state) (commented),
    • or calling the form submit function directly mymodule_form_id_submit(NULL, $form_state);?
  • Is there any better way to do it?
  • Is there any Drupal way to do it?

I have seen that this is a frequently asked question among the Drupal developers. So, maybe there is any standard answer for it.

1

This might help in terms of understanding Rules: https://drupal.org/node/878718

And in terms of flow, I think you could use a rule via the Rules module to direct them to a login page specific to them (people who have just filled out the form), and then the Login Redirect module to redirect them back to the form/submit action after they login. That's assuming the information they entered would remain in the form. You'll probably have to do some experimenting with that.

Jennifer

| improve this answer | |
0

You could set up Rules for this.

Use the Rules module for getting this thing done.

| improve this answer | |
  • Easier said than done. I am not familiar with rules and I don't know how to do it. Can you explain how? – dashohoxha Jan 18 '14 at 16:29

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.