I have a Custom module that implements a block how do I programmatically assign it to a specific role or roles in drupal 8?


This is how I did it.

function _<module-name>_check_user_access() {
  $roles = [

  $current_role = \Drupal::currentUser()->getRoles();
  foreach($roles as $role) {
    if($role == $current_role[1]) {
      return TRUE;

  return FALSE;

See my blog post for more information about it

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.