2

My site has a front page, an 'about us' and 'T&C' pages which are all created as 'basic page' types, and I want to allow anonymous users to access them. I have tried this hook below, but it does not seem to work because I am always redirected to the login page (using LoginToboggan if that matters).

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

  if ($type == 'basic page' && $op == 'view') {
    return NODE_ACCESS_ALLOW;
  }
  else {
    return NODE_ACCESS_DENY;
  } 
  return NODE_ACCESS_IGNORE;
}
1

If you need content access to roles by content types, you can try using Content Access module.

This module allows you to manage permissions for content types by role and author. It allows you to specifiy custom view, edit and delete permissions for each content type. Optionally you can enable per content access settings, so you can customize the access for each content node.

1
  • I had seen it, thanks, but I want to try to avoid adding a new module where I could write ten lines of code for the same result (hopefully). – Pierre_colin Oct 5 '16 at 9:25
0

Type is the machine name of the content type and content type name with space is replaced by _ . And drupal default basic page machine name is page . Please try the below code :

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

  if ($type == 'page' && $op == 'view') {
    return NODE_ACCESS_ALLOW;
  }
  else {
    return NODE_ACCESS_DENY;
  } 
  return NODE_ACCESS_IGNORE;
}
2
  • Thanks, I did not know that 'basic page' type is actually called 'page' by Drupal. It still does not work though. – Pierre_colin Oct 5 '16 at 10:43
  • @Pierre_colin If this doesn't work, you may have some other code preventing the node access. – David Thomas Oct 5 '16 at 10:51

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.