I've to redirect users, after updating a node, to /node/NID path and not to /node/NID/latest (I'm using content moderation).

I've tried to add this on hook_node_update() but have been ignored (without warning or errors):

if (!$entity->isNew()) {
    $path = URL::fromUserInput('/node/' . $entity->id())->toString();
    return new RedirectResponse($path);
  • Better to add this in hook_form_alter as a new submit handler, and then redirect inside of that. – Kevin Aug 16 '18 at 11:45
  • Yes, and then don't return or send the redirect response, set the redirect in $form_state. Node forms often have set a destination too, then you need to handle both, see drupal.stackexchange.com/questions/250465/… – 4k4 Aug 16 '18 at 12:00
use \Drupal\Core\Url;    
use Symfony\Component\HttpFoundation\RedirectResponse;

function MYMODULE_node_update(Drupal\node\NodeInterface $node) {
  $path = Url::fromUserInput('/node/'.$node->id())->toString();
  $response = new RedirectResponse($path);

You have to use like above. You are just returning the redirect response. You need to send redirectresponse to given path.

| improve this answer | |
  • Sending a response is not a good idea, see the duplicate question. – 4k4 Aug 16 '18 at 12:02
  • Suggest of Ajay works but I'll take a look to the duplicate questio, thanks! – arrubiu Aug 16 '18 at 12:04

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