2

I added extra field "field_password_changed" 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(&$form, FormStateInterface $form_state) {

  $form_state->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["field_password_changed"]["#default_value"] = '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?

3
  • hi, you can achieve that with adding custom submit handler in hook_form alter. $form['actions'][$action]['#submit'][] = 'mymodule_submit'; and than change the value there
    – Ales
    Jul 29 '20 at 14:53
  • @Ales – It looks as if the custom submit handler is not the problem. How to store the data is the problem.
    – leymannx
    Jul 29 '20 at 20:58
  • Exactly, my custom handler is called well.
    – MilanG
    Jul 30 '20 at 6:38
4

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 "inject" the data before the user entity gets saved.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_user_form_alter(&$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(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form_state->setValue('field_password_changed', [['value' => '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.

2
  • Actually I tried that module first and even on module page they state that it can display time of last password change I didn't see any settings related to that not that information appears anywhere on form. And I saw that somebody else commented that this module is not providing that feature.
    – MilanG
    Jul 30 '20 at 6:41
  • @MilanG – Yeah, it doesn't come with a field to the user entity, but the information is there in the database. It wouldn't be too difficult to provide a pseudo field on the user page that fetches this information from the database and displays it (but you won't be able to manually edit it on the user edit page, it's just for displaying the information). Let me know if you are interested on that, and I see if I can find the time today to update my answer. I think it's better to leave the business logic to the contrib module and then only add the pseudo field yourself.
    – leymannx
    Jul 30 '20 at 7:03
1

@leymannx's answer is correct and by following his instructions and also by looking into "Force password change" module code I came to following solution:

function user_form_submitted(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $current_pass = $form_state->getValue('current_pass');
  if($current_pass && $current_pass != $form_state->getValue('pass')) {
    $form_state->setValue('field_password_changed', [['value' => date("Y-m-d")]]);
  }
} 

In case someone else needs similar feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.