0

I followed the webform cookbook and created a custom module to "automatically create a Webform node when adding a Webform". Moreover, I need to redirect to the Webform Node at the same time, so I add the "RedirectResponse" code, but it didn't work. Following is my code:

function webform_chshb_webform_insert(\Drupal\webform\WebformInterface $webform) {
    $webformnode = \Drupal\node\entity\Node::create([
        'type' => 'webform',
        'title' => $webform->label(),
        'webform' => ['target_id' => $webform->id()],
    ]);
    $webformnode->save();
    drupal_set_message(t('Node %label created.', ['%label' => $webformnode->label()]));

    $nodeid = $webformnode->id();
    $response = new RedirectResponse("/node/$nodeid/edit");
    $response->send();
}

Thank you.

closed as unclear what you're asking by kiamlaluno Jul 29 at 12:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    You shouldn't send a redirect from an entity CRUD hook, because entity operations run on a different level than user interactions. Move the code to a submit callback of the webform config form and set the redirect in $form_state. – 4k4 Jul 29 at 6:49
-2

try it with return

function webform_chshb_webform_insert(\Drupal\webform\WebformInterface $webform) {
    $webformnode = \Drupal\node\entity\Node::create([
        'type' => 'webform',
        'title' => $webform->label(),
        'webform' => ['target_id' => $webform->id()],
    ]);
    $webformnode->save();
    drupal_set_message(t('Node %label created.', ['%label' => $webformnode->label()]));

    $nodeid = $webformnode->id();
    $response = new RedirectResponse("/node/$nodeid/edit");
    $response->send();
    return;
}

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