Hi,

I have used hook_form_alter to add some fields to the registration form, I am also wanting to redirect to a particular thank you page. I render the form in a theme function.

Brief code as follows

function mymodule_theme(){
  return array( 'myuserregisterform' => array( 'arguments' => array('form' => NULL) ) ) ;
}

function mymodule_form_alter( &$form, $form_state, $form_id ) {
  if ( $form_id == "user_register" ){
    $form['additionalfieldset1'] = fieldset and control arrays ;
    $form['additionalfieldset2'] = fieldset and control arrays ;
    $form['#theme'] = 'myuserregisterform';
    $form['#redirect'] = 'mythankspage' ;
  }
}
		
function theme_myuserregisterform( $form ){
  $html  = drupal_render( $form['additionalfieldset1' ] );
  $html .= drupal_render( $form['additionalfieldset2' ] );
  // render rest of form to get standard token and id
  $html .= drupal_render( $form ) ;
  return $html ;
}

Form rendering works perfectly as expected, however after pressing submit, the details are submitted and the user is registered, but the redirect does not kick in, I go to my home page. This only seems to have happened since adding in my own theme function for the form.

Any ideas?

Thanks,
Keith

Comments

maori’s picture

heya

not sure but with my redirect i used

$form_state['redirect'] = 'mypage';

Keith Hurst’s picture

Not sure but I presume your form_state redirect would have been in your submit handler?

Drupal docs plainly state putting #redirect at the form level on construction or hook_form_alter.

Thing is, the redirect used to be in the hook_form_alter as it is now and was working perfectly.. this only kicked in when I added the theme_form function.

Am I not rendering something in there I should be?

K

kurth’s picture

I've run into the same problem as you describe and have been going around for a while trying to figure out what could be wrong with my theme function. Any tips you might have would be great. :]

Thanks.
~k

Update: I found my issue. In my case I had been rendering only portions of my $form and I did not render the rest of my form. See below:

<?php
	$output .= drupal_render($form['order']);
	$output .= theme_table($table_header, $rows, array('width' => '100%'));
	$output .= drupal_render($form['submit']);
?>

So I added;

<?php
	$output .= drupal_render($form);
?>

To the last line and my redirect worked.

Hope this helps the next googler. :]