Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

Code snippet that can be used to to disable access to known user pages in Drupal 8.


use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\UserInterface;

 * @param UserInterface $entity
 * @param $op
 * @param AccountInterface $account
 * @return AccessResult|\Drupal\Core\Access\AccessResultForbidden|\Drupal\Core\Access\AccessResultNeutral
function mymodule_user_access(UserInterface $entity, $op, AccountInterface $account) {
  if ($entity->id() == $account->id()) {
    switch ($op) {
      case 'update':
      case 'view':
      case 'user_name':
      case 'user_mail':
      case 'user_pass':
      case 'user_edit':
      case 'user_delete':
        $user_roles = $account->getRoles();
        if (!in_array('administrator', $user_roles)) {
          return AccessResult::forbidden();
  return AccessResult::neutral();


Add new comment

Restricted HTML


Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.


Contact Us

All the content is FREE but I still need your help


Become a patreon