All form alter hooks should be executed after all other form alter hooks.
Currently, we use hook_module_implements_alter()
to run the form alter hook after all other hooks from other modules.
But it's not working for form alter hooks implemented in themes because they are executed after module hooks:
\Drupal\Core\Form\FormBuilder::prepareForm();
$this->moduleHandler->alter($hooks, $form, $form_state, $form_id);
$this->themeManager->alter($hooks, $form, $form_state, $form_id);
To fix it, we should hide all fields in #after_build
callbacks.
e.g.
function simplify_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$fields = _simplify_get_config_value('simplify_users_global');
simplify_hide_fields($fields, $form);
}
should be replaced with
function simplify_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#after_build'][] = 'simplify_form_user_register_form_after_build';
}
function simplify_form_user_register_form_after_build($form) {
$fields = _simplify_get_config_value('simplify_users_global');
simplify_hide_fields($fields, $form);
return $form;
}