We give the right to choose access to the page using Rules

When creating a new page or node on Drupal, I would like to restrict access to it. It will be about empowering this with the Rules module .

Checkboxes for setting access to a page on Drupal

A few words about the Rules module . This is a great add-on for CMS Drupal . If your project does not have enough logic of behavior at various events - try this module.

On the performance side, the Rules module does not significantly slow down the work with databases. Therefore, if you have a fairly serious resource project, you will have to write a lot for yourself. In my case, it is enough to fine-tune the module and add a field in the desired content type.

To immediately cut off questions on similar modules, such as Content Access, I’ll clarify that the task is to restrict access to a specific page. In the same Content Access, it is possible to control access to the "content" type. That is, to restrict access to users to the entire contents of the site or to deprive certain rights (editing, deleting).

Now that we have discussed the problem and decided which way to accomplish it, we will proceed with the implementation.

Preparation - module installation

First you need to install the Rules module . The installation process is standard, as with all Drupal modules, it makes no sense to describe it. All work is done under the Drupal 7 version and you should download the module of the same edition. You will need to install the Entity module . We put and activate all the modules. Make sure they are active:

Module activation

Rules module setup

Go to the settings of the module Config-> Process-> Rules . In the first Rules tab, create a new rule.

Rules module - setup

Fill in the required fields - rule name, machine name. We indicate the event on which our rule will be triggered - Content is viewed. Set the content type - Page. We save the rule and close it temporarily.

Adding a custom field - Access to the material

To add a field, let's go to the section Structure -> Content type -> Page -> Manage fields . Add a new field “List (text)” (List with text), set the name “Access to the material”, machine name “field_view_access” and indicate the choice via checkbox (check boxes). Click save. The list field settings window opens. Here we can indicate a hint - help text. Add the values ​​to the list:

Material Access Field Values

Make this field mandatory and set the first value “Visible to everyone” by default. This will save you time in the future.

Material Access Field Values

We save the field settings and they will already appear when editing the "Pages" of the site, though they do not produce any logic.To prevent this field from appearing as text on pages, click in the same section of editing the material type “Page” -> Display Control. Set the hidden display and no text will be displayed from the new field.

Binding a custom field to a rule

So, the final step is to bind our field and display information to the user.
We proceed, again, to the editing of our rule (see paragraph 1). Add condition to the rule:

Setting conditions in Rules

1. NOT Text comparison - Says that the contents of the field should not have the specified value. Negation can be set by a checkbox at the very bottom of the condition parameters. The [field-view-access] field is specified, and the value is 0. That is, if only administrators are given access to the page, this condition will work.
2. AND - Logical operator, added by the button below, “Add and”. For it to work, you need to drag the necessary conditions into child items.
3. NOT User has role (s) - When a user does not have a developer role, this condition also works. Everything is simple here.

Now what will happen - Actions:

Setting actions in Rules

1. Show a message on the site - We show a message with the error class, the text is specified in the parameters.
2. Page redirect - A simple 301 redirect so that the user can continue to travel around the site.

We keep our rule and rejoice.

Waiting for your comments.

All successful development of web space.

Also popular now: