I have some code for a custom module that I'm stuck with.

<?php
function node_access_byregdate($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;
}

The moduel loads fine but does not take the desired actions. In short I'm trying to:

- Restrict view access to certain custom node types by the date the user registered
- 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
- 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 and works well.

If anyone can help with some suggest that would be greatly appreicated. My skills in writing code on a scale of 0-10... 1.

I have more detail on the stackexchange question: https://drupal.stackexchange.com/questions/146520/restrict-node-view-to-...

Thanks in advance.

Comments

kunalkursija’s picture

Hi BrendanP,

i beleive you are trying to do this by hook_node_access(), which is the right way.

But your hook_node_access implementation is not the desired one.

For-Example :
1) if your module name is

"regdate"

2) Your

"hook_node_access"

representation will look like...

  function regdate_node_access($node, $op, $account) {

  }

To get the working representation, you can refer to Example Module Code-Piece

BrendanP’s picture

Thanks very much. That's got it "working" in that it now restricts all content by date, even anonymous users. I need it to only affect content managed by nodeaccess though.

If this is beyond the scope of help that can be offered here please do say.

What I'm need to do is:
a) Allow all users to see standard content types of any date range, i.e. no restriction
b) Content restricted by nodeaccess module and user registration but still function as it is. This works just fine as it is. Certain node types can be assigned to be viewed by restricted user groups or individuals.
c) I need an additional layer over and above what nodeaccess is doing, here I need to restrict the content in "b" further by date the user the has registered. I.e. anything before must be hidden, anything after is fine (so long as it's made available via nodeaccess to that user/group)

More detailed description here > https://drupal.stackexchange.com/questions/146520/restrict-node-view-to-...

Thanks in advance.

karthid’s picture

BrendanP’s picture

Thanks for the input folks. As I mentioned my coding ability is near non-existant so I not been able to make any progress on this. However the requirment is no longer required so all worked ok in the end.

Jaypan’s picture

Comment removed.