0

I've been trying to ajaxify the node creation form, the purpose?

  • Save the node and remains in the node creation page (Done)
  • If the user clicks the save button again save a new node

The second point is not working yet because once the Ajax request is sent again the previous created node is updated.

I tried with:

function saveAjaxForm(&$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new AlertCommand('Saved'));

  return $response;
}

It works, but other #submit callbacks in the form aren't called again.

How can I "execute again" all the submit functions in the form?

  • 1
    Rather than Ajax why not use Drupal's destination=... query parameter to redirect back to the add node form? This will start a fresh node generation form. – CG Monroe Oct 2 at 22:07
  • The idea is to keep the information to add a new node (maybe with small changes) – anacona16 Oct 2 at 23:39
  • 1
    For some reason the Ajax response wasn't updating the form_build_id info, so I added a new Ajax Command: $response->addCommand(new UpdateBuildIdCommand($old_form_build_id, $new_form_build_id)); it solves the issue – anacona16 Oct 3 at 2:46
  • Great, do you mind posting this and the final code as answer? Especially where you get the form build IDs from, probably $form. – 4k4 Oct 3 at 7:54
1

For some weird reason the Ajax response wasn't updating the form_build_id value and the request takes it as the same form, so I added a new Ajax Command:

function saveAjaxForm(&$form, FormStateInterface $form_state) {
  $old_form_build_id = $form['#build_id'];
  $new_form_build_id = $old_form_build_id.rand();

  $response = new AjaxResponse();

  $response->addCommand(new UpdateBuildIdCommand($old_form_build_id, $new_form_build_id));
  $response->addCommand(new AlertCommand('Saved'));

  return $response;
}

It solves the issue.

Your Answer

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

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