5

I need to simply update some user fields from a custom module form.

I try this but it doesn't works :

/**
 * Submit the register form
 *
 * @param array $form
 * @param FormStateInterface $form_state
 * @return array|RedirectResponse
 */
public function submitForm(array &$form, FormStateInterface $form_state)
{
    // Create a new user
    $values = $form_state->getValues();

    // User from Drupal\user\Entity\User;
    $user = $this->ctrUserManager->getCurrentUser();

    if($user){

        $user->set('mail', $values['mail']);
        $user->set('field_last_name', $values['field_lastname']);
        $user->set('field_first_name', $values['field_first_name']);
        $user->set('field_civility', [$values['field_civility']]);

        $violations = $user->validate();

        if (count($violations) === 0) {
            $user->save();
            \drupal_set_message('Your profile has been updated');
            return $form;
        }
    }

    \drupal_set_message('Error during user profile update', 'error');

    return $form;
}
6

you are not saving the user correctly, you should call:

$user->save();

This is an example how to change the username:

$user->setUsername('new-username');
$user->save();

Here is another example to add a role to a user:

$user->addRole('administrator');
$user->save();

Can you try the following:

/**
 * Submit the register form
 *
 * @param array $form
 * @param FormStateInterface $form_state
 * @return array|RedirectResponse
 */
public function submitForm(array &$form, FormStateInterface $form_state)
{
    // Create a new user
    $values = $form_state->getValues();

    // User from Drupal\user\Entity\User;
    $user = $this->ctrUserManager->getCurrentUser();

    if($user){

        $user->set('mail', $values['mail']);
        $user->set('field_last_name', $values['field_lastname']);
        $user->set('field_first_name', $values['field_first_name']);
        $user->set('field_civility', [$values['field_civility']]);

        $violations = $user->validate();

        if (count($violations) === 0) {
            $user->save();
            drupal_set_message('Your profile has been updated');
        } else {
            drupal_set_message('Error during user profile update', 'error');
        }
    }
}
  • Sorry, I have tested that, but formerly it was $user->save(), but still doesn't work – injetkilo Jan 30 '17 at 15:42
  • do you get a $user object from your controller? Are the violations on which you test equal to 0? Not sure why you are returning the $form variable in your submit function, this is not needed. – Fons Vandamme Jan 30 '17 at 15:48
  • Yes I have a user object corresponding to the current drupal user : User::load(\Drupal::currentUser()->id()); and I return the form to avoid to send the second message if user is not defined – injetkilo Jan 30 '17 at 16:05
  • Your code doesn't work either :/ – injetkilo Jan 30 '17 at 16:08
  • Ok I've managed to solve the problem, it was not inside this method but in the buildForm => using #value instead of default_value :-D. Thanks for your time – injetkilo Jan 30 '17 at 16:37
1

Ok ! I manage to solve my problems, in my buildForm I was using #value instead of #default_value !

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.