0

I have a node type question that I want users to be able to create rapidly. So, after adding a question, I want them to be redirected to /node/add/question to immediately do it again.

This is my code so far:

function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'node_question_form':
      foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
          $form['actions'][$action]['#submit'][] = 'MYMODULE_node_form_submit';
        }
      }
      break;
  }
}

function MYMODULE_node_form_submit(array &$form, FormStateInterface &$form_state) {
  // https://drupal.stackexchange.com/a/250474/5147
  \Drupal::request()->query->remove('destination');
  $form_state->setRedirect('node.add_page');
}

This redirects to /node/add, but I want to redirect to /node/add/question. I tried digging through the route options in PHPStorm but I couldn't find anything that gets to the create page for a custom content type.

1
  • 1
    A good way to learn about specific routes is to look at the module's .routing.yml file.
    – sonfd
    Commented Jul 1, 2020 at 11:50

2 Answers 2

1

You're looking for the node.add route, which has a node_type parameter.

$form_state->setRedirect('node.add', ['node_type' => 'question']);
2

You have to redirect to the "add page" of type question,replace

$form_state->setRedirect('node.add_page');

by

$form_state->setRedirect('node.add', ['node_type' => 'question']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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