Getting the current user role in code is quite easy, here's the snippet.

$roles = \Drupal::currentUser()->getRoles();

if(in_array('editor', $roles)) {
  // Do something if user has role editor
}

 


Saved you some valuable time?

Donate and get more free time-saving code on this website in the future.