Hello,

After submitting a form, I want my users to be redirected to another form if a checkbox is checked. The drupal path I want to redirect to is 'node/add/organization' . So far my checkbox displays fine. I am using hook_form_alter() to add the new checkbox field and include my custom function in the $form['#submit'] array of functions.

Here's my custom function's code:

function my_custom_function($form, $form_state) {
    if (isset($form['redirect_checkbox']['#value']['redirect'])) {
        $form_state['redirect'] = 'node/add/organization';
    }
}

This however is not doing the trick.

Some more info I found hacking around my local development environment:
- I used the watchdog() function to see what functions are being called when I click on submit. Here's what I got (in order of execution) : node_form_validate, node_form_submit, menu_node_form_submit, my_custom_function
- I also add a watchdog to see what parameters are going to drupal_redirect_form() and saw that it was being called with $form_state['redirect'] = 'node/222'

Any help would be appreciated, thanks.

Comments

jason_gates’s picture

Hi,

function my_custom_function($form, &$form_state) {
   
   if ($form_state['values']['redirect_checkbox'] == 1) {
      $form_state['redirect'] = 'node/add/organization';
   }
}

Remember, if I've solved your issue. Please prefix your original post subject with [SOLVED]. If not, please post your form alter code so we can see what you are doing :)

The default value for a check box (checked state ) is 1. You can change that in your form definition (or alter) by using the #default_value element. http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

I set up a quick test case and then ran it through my debugger. Developing via print statements lengthens the development cycle exponentially :)

Hope that helps.
Jason

technicalknockout’s picture

Hi Jason,

Thanks again for looking at the code. I know I'm getting in the if block because I ran watchdog inside it and saw the output in the 'admin/reports/dblog' page. Here is what's in the hook_form_alter() if it sheds any light:

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
    // ... other cases for other forms
    case 'mycontenttype_node_form':
      unset($form['#redirect']); // in case it's set, I understand this would override $form_state['redirect']
      $myoptions = array('redirect' => t('Check off this box to be redirected to \'node/add/organization\' after submitting this form');
      $form['redirect_checkbox'] = array(
        '#type' => 'checkboxes',
        '#options' => $myoptions,
        '#weight' => 0,
      );
      array_push($form['#submit'], 'my_custom_function');
      break;
    // some more cases for some other forms ...
}
?>

I've also tried variations placing my funciton at the beginning of the $form['#submit'] array to no avail. Now I'm trying to replace $form['#submit'] altogether and calling node_form_submit($form, $form_state) within my page, but that ends up in a 'server unavailable' message from the browser, not drupal... no php erros in my console...

- - turpana.com - -

jason_gates’s picture

Hi.
Your post says check box, not a check box group. Check box is documented here: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.... .

Try this:

$form['redirect_checkbox'] = array(
   '#type' => 'checkbox',
   '#title' => t('Check off this box to be redirected to \'node/add/organization\' after submitting this form'),
  );

As I previously mentioned, by default Drupal sets the default value of a check box (in checked state) as 1. You can use the #default_value element to change the value returned by the checked state.

Also, please verify (by using your web browser) that the path you are trying to redirect actually renders a page (with form).

Hope that helps.
Jason

technicalknockout’s picture

thanks, but I'm sure that the code inside my custom function is running because I used the following and saw my message output to the log:

<?php
function my_custom_function($form, $form_state) {
    if (isset($form['redirect_checkbox']['#value']['redirect'])) {
        $form_state['redirect'] = 'node/add/organization';
        watchdog('mymessage', 'executing within the if block of my custom function');
    }
}
?>

and I have also been to the path that I'm redirecting to with my browser. It is a form to add a node of type 'organization'. The path is 'node/add/organization'.

The issue I don't understand is why $form_state['redirect'] is not actually redirecting even though the line of code is running. I've tried running it without any if block also and it does not redirect. Is that the correct format for redirecting from a submit function? Can't find anything saying otherwise in the documentation.

- - turpana.com - -

nagarajanl’s picture

Can you make sure that the $form_state is a reference variable (ie) instead of

function my_custom_function($form, $form_state) {

// try this

function my_custom_function($form, &$form_state) {
if (isset($form['redirect_checkbox']['#value']['redirect'])) {
$form_state['redirect'] = 'node/add/organization';
watchdog('mymessage', 'executing within the if block of my custom function');
}
}

Hope this helps...

technicalknockout’s picture

Yes, I had &$form_state in the code, but copied by hand to the forum but mistyped it. That doesn't fix the issue I'm having.

- - turpana.com - -

Bagz’s picture

I could be barking up the wrong tree, but to make the re-direction work, should you not have

drupal_redirect_form($form_state);

as the last statement in your if{} block? (assuming this function is the last thing you do in the submit code)

technicalknockout’s picture

I'm not sure what's happening, but I tried adding drupal_redirect_form($form, $form_state) (as per drupal_redirect_form drupal 6 api) at the end of my submit function (which is running last) but then my node is not saved. I added the 2 watchdog lines to form.inc to see what's being called. One just within the foreach $handlers ... and one just after it. Here's the code:

<?php
function form_execute_handlers($type, &$form, &$form_state) {
  $return = FALSE;
  if (isset($form_state[$type .'_handlers'])) {
    $handlers = $form_state[$type .'_handlers'];
  }
  elseif (isset($form['#'. $type])) {
    $handlers = $form['#'. $type];
  }
  else {
    $handlers = array();
  }

  foreach ($handlers as $function) {watchdog('mymessage', ' *** ' .$function . ' pre execution ***');
    if (function_exists($function))  {
      // Check to see if a previous _submit handler has set a batch, but 
      // make sure we do not react to a batch that is already being processed 
      // (for instance if a batch operation performs a drupal_execute()).
      if ($type == 'submit' && ($batch =& batch_get()) && !isset($batch['current_set'])) {
        // Some previous _submit handler has set a batch. We store the call
        // in a special 'control' batch set, for execution at the correct
        // time during the batch processing workflow.
        $batch['sets'][] = array('form_submit' => $function);
      }
      else {
        $function($form, $form_state);
      }
      $return = TRUE;
    }
  } watchdog('mymessage', ' *** ' .$function . ' post execution'.$return.' *** ');
  return $return;
}

?>

and this is the output to the log:

Type Date Message User Operations
mymessage 28 Jan 2011 - 2:11pm *** node_form_submit post execution1 *** member
content 28 Jan 2011 - 2:11pm profile: updated member. member view
mymessage 28 Jan 2011 - 2:11pm *** my_custom_function post ... member
devel 28 Jan 2011 - 2:11pm adding redirect member
mymessage 28 Jan 2011 - 2:11pm *** my_custom_function pre execution *** member
mymessage 28 Jan 2011 - 2:11pm *** menu_node_form_submit pre execution *** member
mymessage 28 Jan 2011 - 2:11pm *** node_form_submit pre execution *** member
mymessage 28 Jan 2011 - 2:11pm *** node_form_validate post execution1 *** member
mymessage 28 Jan 2011 - 2:11pm *** node_form_validate pre execution *** member

It reads in reverse chronological order, since the log is ordered from newest to oldest. Here's what I don't understand:
The *** node_form_submit pre exececution *** message is logged before my_custom_function is called, but *** node_form_submit post execution is called after my_custom_function is called. Does anyone understand why this is the case? Even though my function is called last, node_form_submit finished executing last. Any ideas???

- - turpana.com - -

technicalknockout’s picture

ahah! hahaha!
here is the answer: http://drupal.org/node/144132#comment-3056754 (buried in a comment to a guide for migrating from 5.x to 6.x)
and the proper custom form_alter code:

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
    // ... other cases for other forms
    case 'mycontenttype_node_form':
      unset($form['#redirect']); 
      $form['cccfield_redirect_checkbox'] = array(
        '#type' => 'checkbox',
        '#title' => t('Check here if you would like to add your organization on the next page'),
        '#weight' => 0,
	);
      $form['buttons']['submit']['#submit'][] = 'cccmodifications_process_profile';
      break;
    // some more cases for some other forms ...
}
?>

The point is to add the custom function to the $form['buttons']['submit']['#submit'] array - not $form['#submit']
Thanks to everyone for your comments!

- - turpana.com - -