简体   繁体   中英

Drupal form redirect to fragment

I have a poll block in the bottom of my front page, and I'd like to redirect the poll form to the block after submit. I use Drupal 6.x. As usual, I use form_alter for this.

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'poll_view_voting':
      // not worked also
      //$form['#redirect'] = url('', array('fragment' => 'poll-block'));
      $form['#redirect'] = '#poll-block';
      break;
  }
}

After submit, I'm redirected to /%2523poll_block (this is a 404 page).

In a preprocess_page function I wrote the $_REQUEST['q'] using drupal_set_message() , and it shows the good redirect (#poll-block), but the URL in browser is encoded.

Can anybody help me?

Thanks in advance.

As per FAPI reference , you can enter an array to be passed to drupal_goto() . Ultimately this goes through url() so use your url() function's ninja tricks here.

Basically you can have 3 types of data here. a string, Boolean FALSE, or an array. If it's a string, it will redirect to the correct location after a url encode (what happens in your case). FALSE will disable redirects, and if you enter an array, it will be passed to drupal_goto().

So for your case, it would be:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'poll_view_voting':
      // not worked also
      //$form['#redirect'] = url('', array('fragment' => 'poll-block'));
      //$form['#redirect'] = array('node/54', array('page' => '2'), 'poll-block'); // No "#" in fragment. This will work.
       $form['#redirect'] = array('', array(), 'poll-block');
      break;
  }
}

However, drupal_goto() usually happens with absolute URL so probably your effort would be useless unless you use javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM