0

When I create a form (using form api) I need, sometimes, to assign a default value to form elements, or to create a list for select elements, under certains conditions. In this cases, I need to do some queries, process, etc. I do it in buildForm method, but I was wondering if It is the right way because this method is called again if I need to rebuild the form due to ajax change (for example with dependant fields).
Until now, I manage it checking $form_state variable for some field in order to see if a field is empty or not, but is it the right way to do it? is there another method to initialize the form elements?

  • Yes, buildForm is the correct place to do that, and $form_state is the correct variable to check for the current state of the form in order to make decisions about which elements to show/hide – Clive Jun 10 at 8:56
  • Yes $form_state->getValue is good or if entity $form_state->getFormObject()->getEntity() – Taggart Jensen Jun 10 at 8:57
  • Ok. Thanks you. I'll continue doing it the same way, but I think It's a little weird. – briast Jun 10 at 17:17

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.