Skip to content

Instantly share code, notes, and snippets.

@juampynr
Last active November 9, 2020 18:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juampynr/660219418bde29b6d107 to your computer and use it in GitHub Desktop.
Save juampynr/660219418bde29b6d107 to your computer and use it in GitHub Desktop.
Make Drupal 7 not to redirect to the node display after saving it

Drupal 7's default behavior is to redirect the user to the full display of a node after it has been saved.

If you want to stay in the edit form after saving, add this snippet to a custom module adjusting the module namespace in the two functions.

For Drupal 8 or 9, have a look at the comment below by @zanvidmar.

Acknowledgements

/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function mymodule_form_node_form_alter(&$form, &$form_state, $form_id) {
$form['actions']['submit']['#submit'][] = '_mymodule_node_submit_redirect';
}
/**
* Extra node form submit handler. Done this way to override the default.
*/
function _mymodule_node_submit_redirect($form, &$form_state) {
if (isset($_GET['destination'])) {
unset($_GET['destination']);
}
$form_state['redirect'] = 'node/' . $form_state['nid'] . '/edit/';
}
@zanvidmar
Copy link

Thank you for the code snippet. I am not sure for which core version this applies. However since $form_state is an object, this code snippet should be updated to:

function _mymodule_node_submit_redirect($form, &$form_state) {
  if (isset($_GET['destination'])) {
    unset($_GET['destination']);
  }

  $url = \Drupal\Core\Url::fromRoute('entity.node.edit_form')
    ->setRouteParameters(['node'=> $form_state->getValue('nid')]);

  /** @var \Drupal\Core\Form\FormState $form_state  */
  $form_state->setRedirectUrl($url);
}

@juampynr
Copy link
Author

juampynr commented Nov 9, 2020

Thanks @zanvidmar! I updated the gist so it states the version and also referenced your comment for Drupal 8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment