I am using Drupal 8. I want to programmatically unselect the View published content checkbox for anonymous users.


How can I achieve this?

1 Answer 1


You have to insert this code in a hook_install() or hook_update() :

$role = \Drupal\user\Entity\Role::load('anonymous');
  • $role = \Drupal\user\Entity\Role::load('anonymous'); $role->revokePermission('view published content'); $role->save(); Oct 21, 2017 at 23:41
  • I have checked in hook_install but it is not working ... Oct 21, 2017 at 23:42
  • The option is still checked for Anonymous user .. Oct 21, 2017 at 23:54
  • 1
    Did you uninstall/reinstall your module ? Your hook_install is in .install file ?
    – guilhom
    Oct 22, 2017 at 0:04
  • 1
    Actually the machine readable name for "View published content" is "access content" I have changed my code now it works like a charm ... Thanks a lot. $role->revokePermission('access content'); Oct 23, 2017 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.