开发者

Drupal hook_form_alter Not redirecting

I am using hook form_alter and setting the $form['#redirect'] = 'dir1/dir2/mypage'. But the form refuses to go there.

Form seems to work otherwise, but keeps sending back to the original form instead of the redirect.

The form I am altering is from the root user module.

mymodule_form_alter(     ){

        ... code...

 $form['account']['pass'] = array(
 '#type' => 'password_confirm', 
 '#size' => 25, '#description' => t(''), 
 '#required' => TRUE
 );

 unset($form['Birthdate']['profile_birthdate']);
 unset($form['Birthdate']);
 unset($form['#action']);

 $form['#validate'] = array('_mymodule_school_registration_validate');
 $form['#submit'] = array( '_mymodule_school_registration_submit');
 $form['#redirect']= "dir1/dir2/mypage";

开发者_如何学编程}

Please help trying to meet an overdue dead line!! : (

Thanks in advance.


Your hook_form_alter() implementation is not correct:

  • Without parameters, you're aren't modifying anything, so none of your changes get registered,
  • $form['#submit'] and $form['#validate'] are already arrays with content, so you should not be resetting them with array(),
  • unsetting $form['#action'] causes the form to do nothing when submitted,
  • setting $form['#redirect'] in hook_form_alter() will get overridden by other handlers, and
  • your hook_form_alter() implementation would affect (and break) every form.

More info: Forms API Reference

Instead, try the following:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id === 'form_id_goes_here') {
    // Code here

    $form['account']['pass'] = array(
      '#type' => 'password_confirm', 
      '#size' => 25, 
      '#description' => t(''), 
      '#required' => TRUE
    );

    unset($form['Birthdate']['profile_birthdate']);
    unset($form['Birthdate']);

    $form['#validate'][] = '_mymodule_school_registration_validate';
    $form['#submit'][] = '_mymodule_school_registration_submit';
  }
}

function _mymodule_school_registration_submit($form, &$form_state) {
  // Code here

  $form_state['redirect'] = 'dir1/dir2/mypage';
}


Try

$form_state['redirect'] = "dir1/dir2/mypage";


If you've only got one handler for your submit you could easily redirect with

function _mymodule_school_registration_submit(..args...) {
...
drupal_goto('somewhere');
}

I think the functionality is the same.

http://api.drupal.org/api/function/drupal_goto/6

I tend to avoid redirects so once you have met your deadline I would refactor your code. You can usually get away with out redirecting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜