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


How can I achieve this?


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(); – Tajdar Khan Afridi Oct 21 '17 at 23:41
  • I have checked in hook_install but it is not working ... – Tajdar Khan Afridi Oct 21 '17 at 23:42
  • The option is still checked for Anonymous user .. – Tajdar Khan Afridi Oct 21 '17 at 23:54
  • 1
    Did you uninstall/reinstall your module ? Your hook_install is in .install file ? – guilhom Oct 22 '17 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'); – Tajdar Khan Afridi Oct 23 '17 at 6:14

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.