function custom_form_node_form_alter(&$form, &$form_state, $form_id) {
if ($form['#node']->type == 'custom') {
$form['actions']['submit']['#value'] = t('Add Entry');
$form['field_custom_email']['und'][0]['value']['#element_validate'] = array('_custom_form_validate_email');
$form['#submit'][] = 'custom_node_submit';
}
}
function custom_node_submit($form, &$form_state) {
$form_state['redirect'] = 'choose/your/path';
}
The code redirects the form independently from the button clicked to submit the form. If you want to redirect a form only when a specific submission button is clicked, then you should used the following code.
function custom_form_node_form_alter(&$form, &$form_state, $form_id) {
if ($form['#node']->type == 'custom') {
$form['actions']['submit']['#value'] = t('Add Entry');
$form['field_custom_email']['und'][0]['value']['#element_validate'] = array('_custom_form_validate_email');
$form['actions']['submit']['#submit'][] = 'custom_node_submit';
}
}
function custom_node_submit($form, &$form_state) {
$form_state['redirect'] = 'choose/your/path';
}
- The form ID doesn't contain hyphens. The form ID is the name of the PHP function that generates the form (a.k.a. the form builder), and a PHP function cannot contain hyphens in its name.
- The form ID is passed to the implementations of hook_form_alter(), hook_form_BASE_FORM_ID_alter(), and hook_form_FORM_ID_alter().
hook_form_FORM_ID_alter()
doesn't really need it, as it is called for a specific form, while the other two hooks are invoked for more than one form.
- The first function is the implementation of hook_form_BASE_FORM_ID_alter(); in this case, the base form ID is "node_form" that is the base form for the node edit form.
- The node edit form contains
$form['#node']
, which is the node object for the object being edited; "custom" is the short ID for the content type you want to redirect the users once they edit a node.
- Altering
$form_state['redirect']
from hook_form_alter()
doesn't have any effect; it needs to be set from a submission handler.