I've use this module in a Drupal 7 project although it's a development release. When I set a node limit on a specific role the total of nodes for all users of that role together seems to be limited. For example if I have user1 and user2 with a node limit of 5 and user1 creates 4 nodes user2 can only create 1 more.

Is this expected behavior or is it something that should be fixed on of the upcoming releases? If it can be fixed and one of the maintainers can point me in the good direction I might be able to solve this issue and share my code.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ricovandevin’s picture

Status: Active » Needs review

I have made some changes to node_limit_role.module and for now this is working how I'd expect it to work. I changed the function node_limit_role_node_limit_sql() to this:

function node_limit_role_node_limit_sql($lid, SelectQuery $select) {
  global $user;

  $limit = node_limit_role_node_limit_load($lid);
  if (empty($limit)) return;

  $select->condition('uid', $user->uid, '=');
}

Can someone please let me know whether I changed expected behavior by this? In that case additional functionality would should be added to the module.

DuaelFr’s picture

Hi Rico,

This was the expected behavior. The one you need is planned for a future release. If you want to provide a sub module I will be pleased to integrate it.

Regards

NathanM’s picture

Subscribing, as this is exactly what I need.

Sinan Erdem’s picture

How can I achieve the same for content type instead of role?

DuaelFr’s picture

I do not understand your need etcetera9, could you be more precise ?

Sinan Erdem’s picture

Of course.

I want to limit the number of contents per user for a specific content type.

For example; I want each user to create only 1 from Article content.

DuaelFr’s picture

You just have to use node_limit_type submodule to do that.
This issue is about limiting each user of a given role instead of all users of this role.

Sinan Erdem’s picture

Node limit type puts a limit for all users combined. For example if you set up a limit of 5, the number of content in the system cannot exceet 5. I want to give each user a right to create 5 contents of that type...

DuaelFr’s picture

Oh ! I missed this point ;)

With this patch you could do what you want. You can use more than one node_limit submodule at a time.
In your case you may combine node_limit_type and node_limit_role (modified as seen above)

Sinan Erdem’s picture

Hehe thanks. I will try it...

grasmash’s picture

That's also the functionality that I was expecting. Has this been implemented in dev?

DuaelFr’s picture

Not yet sorry :/

jdrichmond’s picture

This isn't a pretty solution, but it worked for me. I used "blog" as the node type here. Feel free to use your own node type.

/**
 * Helper function to check limit violations for this node.
 * Always returns FALSE for user 1.
 *
 * @param $node
 *   The node to check.
 */
function _node_limit_violates_limit(&$node) {
  global $user;
  if ($node->uid == 1) {
    return FALSE;
  }
  $limits = node_limit_limits($node);
  foreach ($limits as $idx => $lid) {
    $limit = node_limit_load($lid);
    if ($limit['nlimit'] == NODE_LIMIT_NO_LIMIT) {
      continue;
    }
    $select = _node_limit_sql($limit['lid']);
    // Comment out line below and write own code for finding total number of nodes.
    //$count = $select->execute()->fetchField();
    
    // Custom code to limit 'blogs' per user.
    $count = 0;
    // Check for user.  Only registered users can create nodes.
    if ($user->uid >= 1) {
       // Write database call for users and nodes.
      $results = db_query("SELECT * FROM {users}, {node} WHERE users.uid=:uid AND users.uid=node.uid AND node.type='blog'", array(':uid' => $user->uid));
      foreach ($results as $row) {
        // Run through the entries and get the total number of nodes.
        $count++;
      }
    }
    
    if ($count >= $limit['nlimit']) {
      return TRUE;
    }
  }
  return FALSE;
}
DuaelFr’s picture

Assigned: Unassigned » DuaelFr
Priority: Major » Normal

A new submodule has been pushed to dev branch
http://drupalcode.org/project/node_limit.git/commit/dc3c193

dougsap’s picture

Assigned: DuaelFr » dougsap
Status: Needs review » Reviewed & tested by the community


Successfully tested the new module node_limit_userofrole.

  • The goal was to restrict the number of nodes that could be created by each user within a role.
  • There were no functional issues. It was tested with multiple users, multiple content types, and various limits.

The Coder module gave 12 minor warnings - mostly:

  • Comment should be read "Implements hook_foo()."
  • Use an indent of 2 spaces, with no tabs...
  • Commits to the Git repository do not require the CVS $Id$ keyword in each file.

Thanks.

DuaelFr’s picture

Thank you for your review.
I just pushed a new commit correcting coding standards.

dougsap’s picture

Status: Reviewed & tested by the community » Needs review

The Coder module now reports no warnings for 'Node Limit User of Role'.

However, the functionality has changed. With various settings, but specifically for Limit=1, Content Type Specified, Users of Role = Authenticated, errors:

Notice: Undefined index: node_limit_role in node_limit_userofrole_node_limit_applies_in_context() (line 16 of/home/foo/sites/all/modules/node_limit/node_limit_userofrole/node_limit_userofrole.module).
Notice: Undefined index: node_limit_role in node_limit_userofrole_node_limit_applies_in_context() (line 17 of/home/foo/sites/all/modules/node_limit/node_limit_userofrole/node_limit_userofrole.module).
Notice: Undefined index: node_limit_role in node_limit_userofrole_node_limit_applies_in_context() (line 18 of/home/foo/sites/all/modules/node_limit/node_limit_userofrole/node_limit_userofrole.module).

It SEEMs to work, at least with some use cases, if I replace index 'node_limit_role' with 'node_limit_userofrole' in lines 16 through 19. Thanks!

dougsap’s picture

Status: Needs review » Needs work

Changing status from 'Needs Review' to 'Needs Work'. My mistake.

DuaelFr’s picture

Status: Needs work » Active

Oh dear ! This was a copy&paste mistake.
I just pushed a patch on git. http://drupalcode.org/project/node_limit.git/commit/8a09341

dougsap’s picture

Assigned: dougsap » Unassigned
Status: Active » Reviewed & tested by the community
FileSize
312.16 KB

The 'Node Limit User of Role' module works great when used to restrict node creation by either Authenticated OR Anonymous users.

If separate restrictions (using different limits) are defined for both Authenticated OR Anonymous users, users of both roles will be limited to creating the same number of nodes (of the specified Content Type). See the attached image for a detailed example.

This is not something which I am personally concerned about. It probably is not a frequent use case. I am not sure if there are any easy workarounds. I am only highlighting it for evaluation.

I am very happy with the 'Node Limit User of Role' functionality provided in in 7.x-1.0-alpha3. It seems to be complete and stable enough that this particular issue could be closed.

Thanks again Edouard!

DuaelFr’s picture

Status: Reviewed & tested by the community » Needs review

Alpha4 contains some improvements to avoid some limits to apply when they would not.

dougsap’s picture

Version: 7.x-1.x-dev » 7.x-1.0-alpha4
FileSize
243.33 KB

Behavior on the compound 'Users of Role' issue (#20 above) is the same in Alpha04 as Alpha03. Thanks.

dougsap’s picture

Status: Needs review » Needs work

I just realized that I should have changed status to 'needs work' with my previous post. Thanks!

chefnelone’s picture

I'm in the same situation @finlet in #1, the function posted in #2 is working for me. Is this commited to dev?

DuaelFr’s picture

You might use node_limit_userofrole submodule chefnelone but be aware of what doug said in #22 if you need to restrict authenticated and anonymous users.

chefnelone’s picture

thanks I'll take a look a the submodule.

  • DuaelFr committed dc3c193 on 8.x-1.x
    Issue #1267694 by rvdvin, DuaelFr : allow to limit node creation for...
  • DuaelFr committed 8a09341 on 8.x-1.x
    Issue #1267694 by dougsap : correcting copy&paste mistake
    
    
  • DuaelFr committed f324fe6 on 8.x-1.x
    Issue #1267694...