2

In short I'm trying to find a solution to:

  1. Restrict view access to certain custom node types by the date the user registered
  2. The user would be able to see restricted content created in the month they registered and any future month while their user account is active, but nothing before the date the account was created
  3. There is existing content restrictions and functionality for the same users on the same content. Only certain roles can see selected node types and this is selected when the user is created (manually). This is managed by https://www.drupal.org/project/nodeaccess

This website has restricted content that only members (registered users) can see.

These fall into several categories and the content is divided into these categories too. Depending on the type of member and what you been given access too, you will only be able to see that content. This is managed by Node Access and works well and as intended.

A new requirement has now popped up after the fact where this content access should be further restricted. It should honour the previously mentioned restrictions managed by nodeaccess but now apply additional parameters. All these are relevant to the month in which the user's account is created:

  1. Restricted content posted before must not be available
  2. Restricted content posted after must be available (and future content too, but still honouring the nodeaccess restrictions)

I'm fairly capable with Drupal dev, but cannot write code or rules which I think this requirement is leaning towards.

Anyone got any suggestion that will work or can perhaps point me in the right direction to get a start?

1 Answer 1

1

You need use this hook https://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/hook_node_access/7

So in your module you need code smth like this

function YUORMODULE_node_access($node, $op, $account) {
  $type = is_string($node) ? $node : $node->type;

  if ($op == 'view') {
    global $user; // get current user
    // check user register date
    if ($user->created < $node->created) {
      return NODE_ACCESS_DENY;
    }
  }

// Returning nothing from this function would have the same effect.
  return NODE_ACCESS_IGNORE;
}
9
  • Thanks. I read up on creating a module and created a new module using the above code. To enable the module I had to remove the last "}" as it threw an error when trying to enable the module. The module is now enabled but seems to have no affect. I.e. "old content", restricted content created before the user creation date is still displayed. I get the following error in the server logs: PHP Parse error: syntax error, unexpected '}' in /sites/all/modules/node_access_byregdate/node_access_byregdate.module on line 18
    – Brendan
    Commented Feb 11, 2015 at 13:43
  • you are right) i fixed code. use it)
    – Gaydabura
    Commented Feb 11, 2015 at 13:45
  • sorry, hit enter before I'd added the full story.
    – Brendan
    Commented Feb 11, 2015 at 13:47
  • i edited code. use it once again. drupal.stackexchange.com/a/146527/10563
    – Gaydabura
    Commented Feb 11, 2015 at 13:49
  • 1
    If you use any extra modules, or need to implement more complex logic , add backend UI, etc, 5 lines of code will not be enough, or you may need to use other hooks, which control access to entity. so, my code its just a very basic idea.
    – Gaydabura
    Commented Feb 11, 2015 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.