I'm looking to limit the number of nodes that a user can create in Drupal 7.

Which hook should I implement to step in before the 'add node' page is shown?


4 Answers 4


Alter the access callback for the menu callback:

function custom_module_menu_alter(&$items){
    $items['node/add/page']['access callback'] = 'my_custom_access_callback';
    // Next line needed for Drupal 7
    unset($items['node/add/page']['access arguments']);

Add a function as your custom access callback:

function my_custom_access_callback() {
   global $user;
   $allowed_limit = variable_get('allowed_limit', 10);

   // If Drupal 6.
   $node_counts = db_result(db_query("SELECT count(*) FROM {node} WHERE uid = %d;", $user->uid));
   // End If

   // If Drupal 7.
   $node_counts = db_query("SELECT count(*) FROM {node} WHERE uid = :uid", array(':uid' => $user->uid))->fetchField();
   // End If

   if ($node_counts < $allowed_limit && user_access("create page")) {
     return TRUE;
     drupal_set_message(t('Print message about exceeded limit and/or user permissions.'));
     return FALSE;

This kind of logic will restrict a user to exceed the limit.

  • Worked Beautifully. Did need to unset access arguments as well. unset($items['node/add/page']['access arguments']); in custom_module_menu_alter
    – Citricguy
    Jul 7, 2011 at 1:28
  • 1
    Lastly for searchers, I also had to change the db_query. $node_counts = db_query("SELECT count(*) FROM {node} WHERE uid = :uid", array(':uid' => $user->uid))->fetchField(); I'm assuming just D7 quirks. :) Thank you for the code block, it was exactly what I needed! @Shoaib Nawaz
    – Citricguy
    Jul 7, 2011 at 1:51
  • 3
    Yeah, the code above is for Drupal 6. I suggest you directly edit your adjustments into the above answer (yes, you can do that). You could for example add both queries, document for which version they are and comment one out. Then this question/answer isn't specific to Drupal 7 anymore :)
    – Berdir
    Jul 7, 2011 at 7:51
  • @Shoaib Nawaz, I just attempted my first ever edit. :)
    – Citricguy
    Jul 8, 2011 at 0:04

The Node Limit project now has a Drupal 7 port as an Alpha release.

The Node Limit module allows administrators to restrict the number of nodes of a specific type that roles or users may create. For example, if a site has an "Advertiser" role that can create "advertisement" nodes, then the node limit administrator can restrict all users in that role to a specific number of nodes. He may also restrict users on a per-user basis.

There is a plan to include Rules support in the near future as well. The documentation could use some work but if you poke through the module it will likely accomplish what you need.


Hook node validate may be more appropriate here. http://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_node_validate/7

That way if you use Services the solution will work, while the one proposed by Shoaib will only work if a user accesses the actual node creation page. If a remote client calls node.save via Services the menu access hack will fail.

  • This solution is recommend than menu altering.
    – Nikit
    Oct 10, 2011 at 9:02

For what it's worth, there's no need to alter the menu and remove the default node access callback.

Drupal provides hook_node_access to be used for this purpose:


 * Implements hook_node_access
function MY_MODULE_node_access($node, $op, $account) {
  // Get node type, can be string or node object
  $type = is_string($node) ? $node : $node->type;
  if ($type == 'type_of_interest') {
    // Check node create limit
    if ($op == 'create') {
      // Check if some condition, limit check, is true
      // omitted for brevity
      // ...
        return NODE_ACCESS_DENY;

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.