emerico
11/22/2017 - 6:11 AM

Redirect back to node edit page Drupal 8

Redirect back to node edit page after node submit and not on preview

<?php

/**
 * Implements hook hook_form_node_form_alter
 **/

function hook_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

    foreach (array_keys($form['actions']) as $action) {
      if(is_array($form['actions'][$action])) {
        $form['actions'][$action]['#submit'][] = 'fwsredirect_remove_destination';
      }
    }
}

function hook_remove_destination(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $userInput = $form_state->getUserInput();
  if($userInput['op'] != 'Preview') {
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node instanceof \Drupal\node\NodeInterface) {
      \Drupal::request()->query->set('destination', 'node/' . $node->id() . '/edit');
    }
  }
}