How can I understand when hook_form_alter() is called? In Drupal 8, hook_form_alter() is invoked multiple times: at page load, submission, after executing custom submission handlers, and back to form action.
How can I see at which stage hook_form_alter() is invoked?
The lifetime cycle of a form and its stages depends on whether the form is rebuilding and using Ajax. Most times you react then on the form action you find in:
$form_state-&gt;getTriggeringElement()
The two different calls between page load and before the first submit is because the original form build is not cached and then has to be built the same way as the first one. Normally you don't care and alter the form build the same way for each call. If you need to make a distinction between both calls you find the raw submitted values in $form_state-&gt;getUserInput()