add role to existing user on hook_entity_presave I'm trying to add a role to a user but I can't save the change.
function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity){
  $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()-&gt;id());
  $user-&gt;addRole('foobar');
}

I would like to save the change, but $user-&gt;save() causes a fatal error.
What would be the correct way?
Note: The error is a whitescreen of death. I guess $user-&gt;save() is for creating new users. I would need to update an existing one.
Code should look like this, as an example:

use Drupal\Core\Entity\EntityInterface;
use Drupal\user\Entity\User;

function mymodule_entity_presave(EntityInterface $entity) {
  if ($entity instanceof AccountInterface &amp;&amp; $entity-&gt;isTranslatable()) {
    $user = User::load($entity-&gt;id());
    $user-&gt;addRole('foobar');
  }
}


Or better yet, hook_ENTITY_TYPE_presave():

use Drupal\Core\Entity\EntityInterface;
use Drupal\user\Entity\User;

function mymodule_user_presave(EntityInterface $entity) {
  $user = User::load($entity-&gt;id());
  $user-&gt;addRole('foobar');
}


Simply using hook_entity_presave will fire for any entity about to be saved. So you either need to check the incoming entity, or use the second hook to isolate it.