Question

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');
  }
}
Was it helpful?

Solution

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.

OTHER TIPS

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';
}

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().

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top