Restricting Access to Content in Drupal 8
One of our OSTraining members wanted to restrict access to certain content on his Drupal 8 site.
To do this in Drupal 8, we are going to use the Content Access module.
To follow along with this tutorial, download and install Content Access. I found no errors while using this module, but please note that currently it is a dev release. You will need to enable both options provided.
- ACL
- Content Access
As soon as you have enabled the Content Access module, you will be greeted with a message asking you to rebuild the permissions. This is because the module has made changes to the way permissions will work.
Now let’s create a new content type that will be restricted to authenticated users only.
- Go to Structure > Content type > Add content type.
- Create the content type that will have restricted access.
- On the main Content types screen click the Manage fields dropdown.
- You will now see that you have an “Access control” option.
- Click the “Access control” link and you’ll see all the ways in which you control access to this content type.
- In this example, I want only registered members to see the content, so I’ll uncheck all the “Anonymous user” boxes.
Drupal and Content Access are smart enough to automatically hide any content (or menu links to that content) from users who don’t have permission to see it.
Here’s an example of my Drupal menu for logged-in users. You can see the “OSTraining content” link.
And here’s that same menu for Anonymous users:
If you need a more refined “content access” the module also allows you restrict access Per content node if you enable this you will need to set more detailed permissions via
- People
- Permissions
Using this module you can quickly set access control roles and restrictions.
One great alternative to Content Access is Permissions by Term which allows you to set permissions per tag i find it easier to restrict certain nodes by typing a tag than it is to edit the specific node specially if like me you have a lot of special nodes with diferent levels of access from all content types.
Thanks for the note, Lee! I’m sure others will find it helpful.
Thanks Lee I will certainly look at that Module.
This module does not seem to work reliably resulting in failures to “rebuild permissions” with error 500s, etc. I wish it would work, but it just does not….
With Drupal 8.1.7
When rebuilding permissions I get this:
An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /batch?id=19&op=do_nojs&op=do
StatusText: Internal Server Error
ResponseText: {“message”:”A fatal error occurred: SQLSTATE[HY000]
It seems to work fine for me still just did a fresh install. Error 500 are very generic errors have you checked the error logs for anymore information?
Nothing useful in the log. It may be a combination of modules that is causing this to fail. apparently I’m not the only one having this issue tho: [url=https://www.drupal.org/node/2719605]https://www.drupal.org/node…[/url]
Working perfectly.Thanks, really cool.
Content Access module.
@alawodetaiwo Great to hear
Hi,
I have custom form build with form api, drupal 8, how do i restrict the access only to logged in users or else redirect to login page ?
This tutorial will do all the content protect … after you’ve created the user
Since this blog comes up high for Drupal 8 and content access, here’s another option — if you want a simple module to restrict access to content types based on role, check out Node View Permissions:
[url=https://www.drupal.org/project/node_view_permissions]https://www.drupal.org/proj…[/url]
It’s much simpler than Content Access — it doesn’t do nearly as much, but it’s also easier to configure and maintain if you just want to control access by content type. (It also has a full D8 release whereas Content Access, as of this posting, is still in dev.)
I’ve used Content Access in several D7 sites and it’s wonderfully powerful, just wanted to offer another option.
I cannot find any Content Access Module dev version for drupal 8. I think it has been withdrawn after some serious bugs have been reported. So I may consider this tutorial as obsolete? With drupal 8 I have so many problems with roles and permissions, I have a multilanguage site, and assigning permissions for a new created role for a translating person is n o t possible. Not possible to grant acces to nodes for translation. Also in assigning roles to new created menues, nothing works. I know D8 is in development. But some things should have been better implemented in the core.
ACL and Content Access seem to have been dropped and do nothing other than create errors beyond drupal 8.1.x.
Am considering the alternative mentioned using [url=https://www.drupal.org/project/permissions_by_term]https://www.drupal.org/proj…[/url]. Maybe that will satisfy my current project needs.
After browsing a while node access modules I found this one: [url=https://www.drupal.org/project/page_access]https://www.drupal.org/proj…[/url]
Seems to do the job well so far.
Links in the comments are crooked, pointing to “proj” or “node” on drupal.org.
Sad, we need those links
Try this: https://www.drupal.org/project/node_view_permissions