3

How can i change status to 0 and update an existing user account?

Here is the code i tried:

$account = \Drupal::currentUser();
$account->status = 0;
$account->save();

It gives me error : Call to undefined method Drupal\Core\Session\AccountProxy::save().

6

The account proxy class doesn't have a method called save. Refer to the API docs.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Session%21AccountProxy.php/class/AccountProxy/8.2.x

What you want is the block() method:

$account->block();

As $account should be an instance of UserInterface, returned front getAccount of AccountProxy. But without seeing the rest of the code its hard to see if this is the right approach for what you are trying to do.

If you could get the user account another way, then you could do:

$account = \Drupal\user\Entity\User::load('uid arg');
$account->block();
$account->save();
| improve this answer | |
  • I am using $account = \Drupal::currentUser(); I am not able to understand can you please elaborate. – neha Jan 13 '17 at 17:11
  • Using $account->block() gives Call to undefined method Drupal\Core\Session\AccountProxy::block() – neha Jan 13 '17 at 17:27
  • Because currentUser returns an instance of AccountProxyInterface. – Kevin Jan 13 '17 at 17:30
  • 2
    Your answer is IMHO missing the critical part: $acount->id() instead of 'uid arg' – Berdir Jan 13 '17 at 19:30

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.