1

I've come across a client requirement in Drupal that is very new to me.

The website is subscriber based, so users pay to subscribe at which point they are granted a subscriber role. They then have access to particular content types that they did not previously have access to. This is all easy enough.

The challenge though is that they require each node to only be accessible 5 times by each user.

The website is running Drupal 7 and presently has both Content Access and NodeAccess modules installed along with Rules and Flag.

So far I've worked out how to block access to an entire content type using Rules and roles. I've also been experimenting with Rules and Flags to block a node. For example:

If a node is flagged 5 times by the same user, it should be blocked.

But it still seems to come back to requiring a role that is not able to access it. It's impractical to have a role for every piece of content that cannot be accessed though. Content Access and NodeAccess also revolve around roles.

It seems like every possibility I come across requires a role in order to prevent access, which just doesn't work for this situation as every user is going to potentially have a number of different nodes they cannot access and they are all going to be different. This severely limits the ability of roles to work as far as I can see.

Does anyone have any ideas that might be able to point me in the right direction to achieve this?

Thanks in advance for your help!

3

You can control it using hook_node_access($node, $operation, $account) for Drupal 7. For Drupal 6 one can use hook_nodeapi() for similar result.

When $operation is view, first update usage index for that $account (you need a database table with nid and uid as keys, and number of uses field) and then test if he still can see it, or no. Return appropriate NODE_ACCESS_* constant. Done.

Of course you will need to write your own module, but I guess it'll take less time to wrap simple function in a module than to hack 3 or 4 modules to work together in a way they was never designed to.

  • Thanks for the direction, I had a feeling it may be something that was going to need to be written especially for it but wasn't sure where to start with that. I'll see what I can come up with. Thanks again! – Matt May 19 '13 at 11:42
  • Notice what reported in the documentation for hook_access(): "This hook allows node modules to limit access to the node types they define." That restriction doesn't apply to hook_node_access(). – kiamlaluno Jun 4 '13 at 8:44
  • See how node_access() invokes the hook: First it gets the module name with $module = node_get_types('module', $node); and then invokes the hook with $access = module_invoke($module, 'access', $op, $node, $account);. If the hook could be implemented by every module, that function would not invoke the hook for just a module. – kiamlaluno Jun 4 '13 at 8:59
  • Ok, corrected my answer to use nodeapi instead. – Mołot Jun 4 '13 at 9:01

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.