Hello!

I am trying to use drupal_goto() inside hook_submit() but when drupal_goto() gets executed the node will not be updated.
Does anyone know how to send the user to another page other than the default node view page after hook_submit() is executed?

Thanks

Comments

N1ghteyes’s picture

can you post the code inside your hook call? :)

merrillbros’s picture

Whoops I am in hook_submit().

So if I take out drupal_goto() the node will update. I put it back in the node will NOT update.

function workflow_submit($form, &$form_state) {
	
	$type = $form_state['values']['type'];
	
	$sig_var = variable_get('signature_formvals_'.$type);
	
	global $user;
	
	$op = $form_state['values']['op'];
	if($op == 'Save'){
		$form_state['values']['field_workflow_state']['und'][0]['value'] = 4;
		if($form_state['values']['nid'] == ''){
			$form_state['values']['field_current_level']['und'][0]['value'] = 1;
		}
	} 
	
	elseif($op == 'Save as Draft'){
		$form_state['values']['field_workflow_state']['und'][0]['value'] = 2;
	} 
	
	global $user;
	$person = user_load($user->uid);
	
	if($form_state['values']['op'] == 'Save' && $user->uid == $form_state['values']['uid']){
		
		// Approver as submitter approval fix
		switch($sig_var['workflow_levels']){
			case 'One':
				if($user->uid == $form_state['values']['field_workflow_approver_1']['und'][0]['uid']){
					//$form_state['values']['field_workflow_approval']['und'][0]['value'] = 'Approved';
				} else {
					$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
				}
			break;
			
			case 'Two':
				if($user->uid == $form_state['values']['field_workflow_approver_2']['und'][0]['uid']){
					//$form_state['values']['field_workflow_approval']['und'][0]['value'] = 'Approved';
				} else {
					$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
				}
			break;
			
			case 'Three':
				if($user->uid == $form_state['values']['field_workflow_approver_3']['und'][0]['uid']){
					//$form_state['values']['field_workflow_approval']['und'][0]['value'] = 'Approved';
				} else {
					$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
				}
			break;
			
			default:
				$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
		}
	}
	
		$level = $form['field_current_level']['und']['#default_value'][0];

		$form_state = signature_signatures($level,$sig_var,$form_state);
		

		//test test
		$message .= 'User:' . $user->uid . '<br>';
		$message .= 'One :' . $form_state['values']['field_workflow_approver_1']['und'][0]['uid'] . '<br>';
		$message .= 'Two :' . $form_state['values']['field_workflow_approver_2']['und'][0]['uid'] . '<br>';
		$message .= 'Thre:' . $form_state['values']['field_workflow_approver_3']['und'][0]['uid'] . '<br>';
		$message .= 'Page:' . $form_state['values']['nid'];
		$email['message'] = $message;
		$email['to'] = 'mmerrill@technicacorp.com';
		workflows_mail_send($email);
		
		// Sends Approvers to eforms page
		$redirect_url = 'node/' . $form_state['values']['nid'];
		if($user->uid == $form_state['values']['field_workflow_approver_1']['und'][0]['uid']){
			$redirect_url = 'form';
		}
		
	drupal_goto('$redirect_url');
}
N1ghteyes’s picture

ah, well this might help :P

    drupal_goto('$redirect_url');

should be

    drupal_goto($redirect_url);

quotes in php can be a bit funny, double quotes will handle the addition of variables, single quotes treat everything as a string, so at the moment your passing $redirect_url as a string, rather than the value it contains. just drop the single quotes from around it (no quotes are needed).

test that out, shout when you have a result or not :), but that's a good place to start.

merrillbros’s picture

Thanks!

drupal_goto($redirect_url);

I changed it and ran it same thing : (

When drupal_goto() gets executed the changes to the node do not get submitted.
But if I comment out drupal_goto() the node will be updated.

Trying to get this redirect to work.

N1ghteyes’s picture

ok, looking through some of the stuff I've done in the past... (bit of a shot in the dark here) it seems that because your calling drupal_goto() before the end of the function (changing the headers), the form_state data is not being stored in the referenced array. try returning drupal_goto()

i say again, this is a bit of a shot in the dark :)
try this:

    return drupal_goto($redirect_url);
merrillbros’s picture

haha we think very alike.

I tried using return earlier and still no avail.
I tried it again and the changes do not get submitted : (

hook_submit needs to return the $node in order to make the updates.

I thought about making another function outside of the hook_submit maybe that will work...
That still doesnt work

function workflow_submit($form, &$form_state) {
	
	$type = $form_state['values']['type'];
	
	$sig_var = variable_get('signature_formvals_'.$type);
	
	global $user;
	
	$op = $form_state['values']['op'];
	if($op == 'Save'){
		$form_state['values']['field_workflow_state']['und'][0]['value'] = 4;
		if($form_state['values']['nid'] == ''){
			$form_state['values']['field_current_level']['und'][0]['value'] = 1;
		}
	} 
	
	elseif($op == 'Save as Draft'){
		$form_state['values']['field_workflow_state']['und'][0]['value'] = 2;
	} 
	
	global $user;
	$person = user_load($user->uid);
	
	if($form_state['values']['op'] == 'Save' && $user->uid == $form_state['values']['uid']){
		
		// Approver as submitter approval fix
		switch($sig_var['workflow_levels']){
			case 'One':
				if($user->uid == $form_state['values']['field_workflow_approver_1']['und'][0]['uid']){
					//$form_state['values']['field_workflow_approval']['und'][0]['value'] = 'Approved';
				} else {
					$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
				}
			break;
			
			case 'Two':
				if($user->uid == $form_state['values']['field_workflow_approver_2']['und'][0]['uid']){
					//$form_state['values']['field_workflow_approval']['und'][0]['value'] = 'Approved';
				} else {
					$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
				}
			break;
			
			case 'Three':
				if($user->uid == $form_state['values']['field_workflow_approver_3']['und'][0]['uid']){
					//$form_state['values']['field_workflow_approval']['und'][0]['value'] = 'Approved';
				} else {
					$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
				}
			break;
			
			default:
				$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
		}
	}
	
		$level = $form['field_current_level']['und']['#default_value'][0];

		$form_state = signature_signatures($level,$sig_var,$form_state);
		

		//test test
		$message .= 'User:' . $user->uid . '<br>';
		$message .= 'One :' . $form_state['values']['field_workflow_approver_1']['und'][0]['uid'] . '<br>';
		$message .= 'Two :' . $form_state['values']['field_workflow_approver_2']['und'][0]['uid'] . '<br>';
		$message .= 'Thre:' . $form_state['values']['field_workflow_approver_3']['und'][0]['uid'] . '<br>';
		$message .= 'Page:' . $form_state['values']['nid'];
		$email['message'] = $message;
		$email['to'] = 'mmerrill@technicacorp.com';
		workflows_mail_send($email);
		
		// Sends Approvers to eforms page
		$redirect_url = 'node/' . $form_state['values']['nid'];
		if($user->uid == $form_state['values']['field_workflow_approver_1']['und'][0]['uid']){
			$redirect_url = 'form';
		}
		
		brc_redirect($redirect_url);
}

function brc_redirect($redirect_url){
	return drupal_goto($redirect_url);
}
N1ghteyes’s picture

Try Nevets solution below, definitely a better way of doing it and should work no problem :)

nevets’s picture

Instead of try drupal_goto('$redirect_url');

$form_state['redirect'] = $redirect_url;
N1ghteyes’s picture

well, that beats my suggestion...

kudos :D

merrillbros’s picture

function workflow_submit($form, &$form_state) {
	
	$type = $form_state['values']['type'];
	
	$sig_var = variable_get('signature_formvals_'.$type);
	
	global $user;
	
	$op = $form_state['values']['op'];
	if($op == 'Save'){
		$form_state['values']['field_workflow_state']['und'][0]['value'] = 4;
		if($form_state['values']['nid'] == ''){
			$form_state['values']['field_current_level']['und'][0]['value'] = 1;
		}
	} 
	
	elseif($op == 'Save as Draft'){
		$form_state['values']['field_workflow_state']['und'][0]['value'] = 2;
	} 
	
	global $user;
	$person = user_load($user->uid);
	
	if($form_state['values']['op'] == 'Save' && $user->uid == $form_state['values']['uid']){
		
		// Approver as submitter approval fix
		switch($sig_var['workflow_levels']){
			case 'One':
				if($user->uid == $form_state['values']['field_workflow_approver_1']['und'][0]['uid']){
					//$form_state['values']['field_workflow_approval']['und'][0]['value'] = 'Approved';
				} else {
					$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
				}
			break;
			
			case 'Two':
				if($user->uid == $form_state['values']['field_workflow_approver_2']['und'][0]['uid']){
					//$form_state['values']['field_workflow_approval']['und'][0]['value'] = 'Approved';
				} else {
					$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
				}
			break;
			
			case 'Three':
				if($user->uid == $form_state['values']['field_workflow_approver_3']['und'][0]['uid']){
					//$form_state['values']['field_workflow_approval']['und'][0]['value'] = 'Approved';
				} else {
					$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
				}
			break;
			
			default:
				$form_state['values']['field_workflow_approval']['und'][0]['value'] = '_none';
		}
	}
	
		$level = $form['field_current_level']['und']['#default_value'][0];

		$form_state = signature_signatures($level,$sig_var,$form_state);
		
		$form_state['redirect'] = 'form';
}

The node will update now but it still goes to node the was being updated.
I read up on $form_state['redirect'] and it sounds like it should do the trick.

When I print $form_state['redirect'] its value is 'form'.....

Any more ideas?
Thanks a lot again.

nevets’s picture

Is 'form' a valid path?

merrillbros’s picture

Yes it is.

I just checked:
$form_state['no_redirect']

It did not have a value...

Also tried this:
call_user_func_array('drupal_goto', $form_state['redirect']);

and this:

$form_state['redirect'] = drupal_goto('form');

From this article:
http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_redir...

nevets’s picture

Looking at node_form_submit() this will not work since it has a hard coded redirect. You would need to use hook_form_alter() to change the submit handler to on you write that replicated node_form_submit() but without the hard coded redirect.

chetan-singhal’s picture

use either
$form_state['redirect'] = $redirect_url;

or

$_GET['destination']=$redirect_url;

drupal_goto is wrong way to accomplish this. If your other module is using node_insert/node_update then this will not work.

chetan

merrillbros’s picture

$_GET['destination']=$redirect_url;

This worked!!!

Thanks everyone for all the help!