0

I have a view of nodes of type task. I created a block with an inline task create form (using form block module) The block is next to the view on the same page.

Question: After creating a new task by using the block, it shows the task full node. Instead, I want it to be redirected to the initial page (the views page) where the task was created.

I understand that the block is not aware of the URL, neither is the task create form in it. That is why a return to previous page in for example business rules, does not work.

How can a redirect be accomplished in this case?


I created a module with this code, which does not work:

<?php

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */


function yc_noderedirect_form_alter(&$form, FormStateInterface $form_state, $form_id) {

if ($form_id == 'node_task_form' || $form_id == 'node_article_form') {

    $current_path = \Drupal::service('path.current')->getPath();
    $form_state->setRedirect($current_path);

  }
}
  • For the previous page you set a destination query parameter. Or do you mean the current page where the block form is placed? Then add a custom submit handler in a form alter hook and redirect when submitting $form_state->setRedirect('<current>');. – 4k4 Apr 16 at 11:34
  • Both can be connected when a destination parameter is already set, see drupal.stackexchange.com/questions/250465/… – 4k4 Apr 16 at 11:35
  • <current> only works in a twig template right? Anyhow, please have a look at my addtion to my question, thanks! – Yuri Apr 16 at 23:02
  • setRedirect() expects a route name and <current> is a route name. You need two functions, the form alter hook where you add the custom submit and in this callback you put the redirect, see drupal.stackexchange.com/questions/234612/… – 4k4 Apr 17 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.