Some possibilities to change drupal forms

There are some possibilities to change the structure of a drupal form. They are trigged at different times in the form building process, and therefor different things can be accomplished with those techniques.

1. hook_form_alter

Runs first. Add properties to the form. Your hook_form_alter implementions run before or after another module depending on the module weight.

2. custom #after_build function (added via a hook_form_alter)

Runs after the form array was built. Runs after hook_form_alter.
See https://api.drupal.org/api/drupal/developer%21topics%21forms_api_referen...

3. custom #pre_render function (added via a hook_form_alter)

Runs just before the form is rendered. Runs after #after_build.
See https://api.drupal.org/api/drupal/developer%21topics%21forms_api_referen...

Example use case

I needed to modify form_error messages after the conditional_fields module has processed the errors. As Conditional fields module does it's validation in an #after_build function, I needed to use a #pre_render function to run after the conditional field code.

Using form_alter alone and modifying the weight of my custom module did not help.