0

How to limit user for adding new nodes?

For example: Users can not Create/Add more than One post for specific Content Type.

2 Answers 2

0

The Node Limit module does exactly that:

The Node Limit module allows administrators to restrict the number of nodes of a specific type that roles or users may create.

Among its features are:

  • Per-role node limits
  • Per-user node limits
  • Per-time interval node limits
  • Any combination of the above
0
1

To limit users from adding more than one node for a specific content type, you can use the hook hook_form_alter() to alter the node form and disable the submit button if the user has already created a node for that content type. Here's an example code snippet:

function my_module_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == 'node_form' && $form['#node']->type == 'my_content_type') {
    $node_count = db_query("SELECT COUNT(*) FROM {node} WHERE type = :type AND uid = :uid", array(':type' => 'my_content_type', ':uid' => $user->uid))->fetchField();
    if ($node_count > 0) {
      $form['actions']['submit']['#attributes']['disabled'] = 'disabled';
    }
  }
}

In this code, you first check if the current form being altered is a node form for our desired content type. Then, we count the number of nodes that have been created by the current user for that content type. If the count is greater than zero, then disable the submit button.

To add a specific limit number for user role type, you can modify the above code to check for the user's role and apply different limits accordingly. Here's an example:

function my_module_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == 'node_form' && $form['#node']->type == 'my_content_type') {
    switch ($user->roles[3]) { // assuming the role ID we want to limit is at index 3
      case 'editor':
        $limit = 5; // limit editors to 5 nodes
        break;
      case 'contributor':
        $limit = 3; // limit contributors to 3 nodes
        break;
      default:
        $limit = 1; // default limit for all other users is 1 node
        break;
    }
    $node_count = db_query("SELECT COUNT(*) FROM {node} WHERE type = :type AND uid = :uid", array(':type' => 'my_content_type', ':uid' => $user->uid))->fetchField();
    if ($node_count >= $limit) { // use >= instead of > to allow users to edit existing nodes
      $form['actions']['submit']['#attributes']['disabled'] = 'disabled';
    }
  }
}

In this code, you first check the user's role and set a specific limit for each role. Then, it count the number of nodes created by the user for the desired content type and compare it with the limit. If the count is greater than or equal to the limit, it disable the submit button.

I hope this help!

1
  • 1
    Thanks for the Help!
    – Barry
    Feb 24, 2023 at 18:40

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.