Proper way to alter field value from submit handler I added extra field &quot;field_password_changed&quot; to user account and I want to store there time of latest password change (simple input field).
Now, I added custom submit handler to catch user account edit action and currently it looks like this:
function user_form_submitted(&amp;$form, FormStateInterface $form_state) {

  $form_state-&gt;set('field_password_changed', 'test22');

  $form['field_password_changed']['en'][0]['value']['#value'] = '54321';
  $form['field_password_changed']['widget'][0]['value']['#default_value'] = 'My new value';
  $form['field_password_changed']['widget'][0]['value']['#value'] = 1;

  $form[&quot;field_password_changed&quot;][&quot;#default_value&quot;] = 'ffff';

}

Basically I tried every suggestion from this page:
https://www.drupal.org/forum/support/module-development-and-code-questions/2013-08-01/using-hook_form_alter-to-set-a-field
And not even one is working form me.
What is the proper way to change form field value in custom submit handler?
Consider using the Force Password Change module which can already keep track of that.

First, add your custom submit handler to the beginning of the submit handlers array and not to the end, to ensure you &quot;inject&quot; the data before the user entity gets saved.
/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_user_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  array_unshift($form['actions']['submit']['#submit'], 'user_form_submitted');
}

Second, depending a little bit on what field type exactly you added to the user entity, the form value needs to be an array of values. In the sample below I used a simple plain textfield to store the data.
function user_form_submitted(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form_state-&gt;setValue('field_password_changed', [['value' =&gt; 'test22']]);
}

Third, you somehow need to check if the password got changed at all or if just the user form got submitted. Again, have a look at the force_password_change.module to get an idea of what exactly you may need to check for.