0

Looking at Drupal code, I noticed the following function.

function _node_add_access() {
  $types = node_type_get_types();
  foreach ($types as $type) {
    if (node_hook($type->type, 'form') && node_access('create', $type->type)) {
      return TRUE;
    }
  }
  if (user_access('administer content types')) {
    // There are no content types defined that the user has permission to create,
    // but the user does have the permission to administer the content types, so
    // grant them access to the page anyway.
    return TRUE;
  }
  return FALSE;
}

What is node_hook()? How can I implement this hook for additional access control?

1 Answer 1

0

node_hook() is not an hook; it's a helper function Drupal uses to verify a content-type hook exists. In the code you are showing, the function is verifying the module implementing the $type->type content type implements hook_form().

To alter the access control for a node, you have two options:

Implementing hook_node_access() is easier, but that hook is not used in node listings (lists of nodes generated from a select query, such as the default home page at path 'node', an RSS feed, a recent content block, etc.).

Notice that, since _node_add_access() is an access callback, you could change the access callback used by node/add. I suggest not doing it because that would by-pass all the node access control implemented by Drupal.

Reference

2
  • hook_node_access() is called on node/add page too? My goal is to check if node/add url has additional GET parameter - a token.
    – Codium
    Commented Sep 10, 2015 at 16:52
  • Yes, it is. In that case, the $op parameter is equal to 'create'.
    – avpaderno
    Commented Sep 10, 2015 at 16:57

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.