Using hook_form_alter()
, you can add a submission handler that sets the redirection. The code for the submission handler would be similar to the following one.
function mymodule_node_edit_submit(array &$form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
$id = \Drupal::request()->query->get('id');
$form_state->setRedirect('entity.node.canonical', ['node' => $node->id()], [query => ['id' => $id]]);
}
The code for hook_form_BASE_FORM_ID_alter()
would simply be the following one.
function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
// Since this hook is invoked for every node, check its content type.
if ($node->getType() == 'the content type you are interested in') {
$form['actions']['submit']['#submit'][] = 'mymodule_node_edit_submit';
}
}
The hook_form_FORM_ID_alter()
implementation should be named mymodule_form_node_<content_type_machine_name>_alter()
. (Replace mymodule with the module machine name, <content_type_machine_name>
with the content type machine name.) In this case, since the hook is invoked only for a content type, it doesn't need to check the node content type before adding the submission handler.