I'm looking to add/edit elements on a Drupal 8 webform with Ajax Callbacks, I've read up on the buildForm technique, however my problem is a little more complicated, in that I am actually trying to do all of this inside of a custom composite element, not a form object.

Adding a new element via HtmlCommand is no problem, however any '#ajax' callbacks I add to this new element do not get initialised.


1 Answer 1


You can't add elements in an ajax callback:

Note, that as part of the Form APIs security system, you cannot create new form elements in the callback function, as they will throw errors upon submission, and any #ajax on elements created in the callback will also not work. If you need to create new elements on ajax submit, they must be added in the form definition.

from https://www.drupal.org/docs/7/api/javascript-api/ajax-forms-in-drupal-7 (still valid for D8).

It should be possible to find a method in the composite element which is part of the form building process where you can add the form element.

  • Yup, I've tried several functions like processWebformComposite but the form_state is never correct, I have even tried to use hooks, but I never receive the correct form_state (rebuild is always false and manually added rebuild info array is empty)
    – user69676
    Apr 24, 2018 at 12:22

Your Answer

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