14

Using hook_form_alter, how can I redirect a user to a a custom page after creating or editing a node instead of the node that was created or edited?

The current code for hook_form_alter() is the following one:

function custom_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'custom-node-form') {
    $form['actions']['submit']['#value'] = t('Add Entry');
    $form['field_custom_email']['und'][0]['value']['#element_validate'] = array('_custom_form_validate_email');
  }
}
1

4 Answers 4

17
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.
1
  • 1
    Thanks for this. Note that the & in function custom_node_submit($form, &$form_state) { is very important. Otherwise you're not passing the state as a reference and the edits you manually do won't take affect.
    – Bram
    Commented Nov 15, 2017 at 7:21
9

This code works on a Drupal 7 site.

/**
 * Implements hook_form_alter()
 */

function custom_form_alter(&$form, &$form_state, $form_id) {    
  if ($form_id == 'article_node_form') {
    $form['actions']['submit']['#submit'][] = 'mysubmit_node_submit';
  }     
}

function mysubmit_node_submit($form, &$form_state) {
  $form_state['redirect'] = '/homepage';
}
4
  • Which answer are you referring as #4?
    – Elin Y.
    Commented Feb 6, 2014 at 15:27
  • why is this not working for me? I am using the same code
    – ruby
    Commented Sep 8, 2014 at 17:22
  • Cleared your cache @ruby ? Commented Feb 16, 2015 at 13:25
  • For existing nodes, you better ensure to unset the destination first, which normally is added to the URL when you click the edit button on the admin/content page: drupal.stackexchange.com/a/192271/15055.
    – leymannx
    Commented Feb 22, 2016 at 14:17
4

This can be done by setting $form_state['redirect'], for example using one of the following lines.

$form_state['redirect'] = "some-internal-path";
$form_state['redirect'] = array($path, $options_array, $http_code);

It's the same arguments you would pass to drupal_goto().

3
  • Thank you for the feedback @Shoaib Nawaz. I added the $form_state['redirect'] to the hook_form_alter hook with out luck.
    – Citricguy
    Commented Jun 30, 2011 at 1:28
  • For the life of me, I cannot get $form_state['redirect'] to take effect anywhere.
    – Citricguy
    Commented Jun 30, 2011 at 8:18
  • This will not work if you try to redirect after a node has been added or edited. To see why, take a look at node form submit. Whatever value you assign to $form_state['redirect'] will be overwritten if ($node->nid) evaluates to TRUE. Commented Aug 16, 2018 at 9:50
2

Not sure if this is an option for you, but if this is a content type you could achieve this pretty quickly by using the Rules module by setting a rule to be triggered on update or creation of a certain content type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.