How to use Drupal rules to adapt content access permissions for nodes that are older than 1 week?

449 views Asked by At

I have a special content type named "example". I want to show new nodes of this type to anonymous users of my site.

What I need: after 1 week the node was created, content access permissions (Content Access module is installed) are changed that only users with particular role are able to see this node.

Should this be triggered on cron or what? Or just how to do something to nodes that are older than 1 week?

Could you provide some instructions on how to do that? Because I'm new to the Rules module and have no any ideas.

2

There are 2 answers

0
Florian Motteau On

You should be able to do this with Rules (see this question, not exactly what you want but close), but I'd go for a tiny custom module implementing hook_cron, where you fetch all nodes with creation date < (now - 1 week), and modify the permissions for each of them.

It should be more efficient than the Rule approach explained in my first link, where you need to loop over all nodes on each cron execution. And Rules can be quite more annoying than writing plain PHP. I prefer learning Drupal API than spending hours clicking in Rules interface (Rules is great, but it's hard).

Good luck

0
Pierre.Vriens On

Yes you should be able to get this to work using the Rules module to implement what you're looking for, but I recommend you to also combine that with the Rules Once per Day and the Views Rules modules, as further explained below.

Step 1: Rules Event

Your question doesn't really specify anything that could/should be used as the Rules Event (for the rule to be triggered. And even though it's like "up to your own imagination" (any Rules Event will do), something that will work for sure is to use the Rules Once per Day module. Here is how it works (as per the comment in issue 2495775, from the module owner):

  • You specify a trigger hour on the administration settings page for this module.
  • The Rule trigger will then run when cron tasks are first run after the start of that hour. The actual run time will depend on your cron task timings.

So this is another way to understand/read this:

  • The "Event" will only be triggered when a cron job is run.
  • And that event will only be triggered 1 time / day, i.e. "next time cron runs after the trigger hour has passed".

Step 2: Rules Actions (and optional events)

Some details about the Views Rules module (from its project page):

Provides Views directly as Rules actions and loops to seamlessly use view result data.

The previous quote may seem a bit cryptic (it may make you think like "so what, how can this help me?"). Therefor some more details about how to move forward using these modules:

  1. Create a view (using Views) so that you have 1 Views result (row) with all the nodes (of at least 1 week old) you want to be processed, whereas that view has fields (columns) for whatever is needed in subsequent steps, eg the node ID, but possibly other fields as well. You'll need these View fields later on as values to be processed by your rule, "to change the content access permissions (using the content_access module) so that only users with particular role are able to see such nodes" (similar to what you mentioned in your question). Important: use a Views display type of "Rules".
  2. Create a custom rule in which you use the Views Rules module to iterate over each of these Views results in a Rules action, using the Rules technique known as a "Rules Loop".
  3. For each iteration step in your Rules loop, perform a Rules Action to "do your thing" (= change the content access permissions). At that point you'll have all data from each column of your Views results available as so called Rules Parameters. So at that point it's a piece of cake to adapt the content access permissions for the node you're processing in that loop.
  4. Optionally, you may also want to add whatever extra Rules Condition(s), also up to your own imagination.

Easy, no?