Limit Node Creation with Node Limit Number and Rules in Drupal


For a website I am building for a client one requirement is that certain roles shall only be able to create a limited number of nodes of certain content types. The website is also going to be available in multiple languages and users shall of course be able to create translations of their own nodes.

At first I thought this was going to be a quite complicated requirement to solve. It turned out to be the complete opposite and ended up being a very elegant solution.

Node Limit Number and Rules

I solved it using the Node Limit Number and Rules modules. Version 2.0 of the Node Limit Number module, currently in beta, requires Rules to work and it does not have any UI of its own anymore like previous versions.

I simply cloned the example rule Node Limit Number comes with and modified it to my needs for this project. The whole thing took about 5 minutes and worked perfectly. Every time a user tries to create a second node of the same type it kicks in and block that. Using rules it is easy to notify why, using a custom message, as well as redirect the user to the first node they created.

The condition rule it adds let you to set a number for the limit as well as a timeframe that it applies to. For the timeframe you can select No Limit, Daily, Weekly, Monthly and Annually.

This gives you great flexibility on how to limit node creations. Sure, a more granular timeframe setting would have been nice, but these do fine for most implementations.

Adding Translation Exception

When I tested adding a translation of the created node I ran into a problem though. Since translations are nodes they are of course also counted. Thus, when clicking the create translation link the rule kicked in and blocked it.

A solution to this was found in this issue ticket for Node Limit Number. You only need to make a group of the node limit rule and add the custom PHP code from that issue comment. You need to have the core PHP Filter module enabled to be able to add it though.

A new test and that problem was solved as well.

Lessons Learned

As usual there is normally a module solving your problem. In my case it turned out to be a very elegant solution that does not add any complexity to the website I am building.

This was the first time I used the Rules module and it didn't took me long to realise how powerful it can be when modules like Node Limit Number adds its features to it.

The result is that you will have a much more manageable website and module maintainers can focus on the core features of their modules instead of having to also add their own custom UI.

The list of modules integrating with Rules is growing. You are most likely already using some of them, such as CCK, Token, Twitter or Content Profile. If you are, it might be useful to investigate how Rules can improve your website too.

Comments

Re: Limit Node Creation with Node Limit Number and Rules in ...

Any idea how to do this in Drupal 7 ? I was thinking if using just Roles is possible to limit a user to create just one node for a specific content type.

Re: Limit Node Creation with Node Limit Number and Rules in ...

Hi Markos,

There is no core function to do this in Drupal 7. I just checked the Node Limit Number project page and there is no mention of a planned D7 port. Since it has existed since Drupal 5 I am quite sure it will happen at some point though. The Rules module is already in alpha for D7 so at least that requirement is fulfilled for it.

You can always file a feature request in the issue queue asking if, and when, a port is planned.

Re: Limit Node Creation with Node Limit Number and Rules in ...

thanks, just posted a feature request as you suggested.

Trying to use Rules - the concep is to trigger an action when a user (with a certain role) tries to create a new node of a certain content type. In case he already created the node I was hoping to redirect him to the edit page of that node.
It turns out this is a big issue for me.

Re: Limit Node Creation with Node Limit Number and Rules in ...

I believe the tricky part for you hear is to find the node the author already created. From what I understand with the Node Limit Number module it checks the content type and the date for previously created nodes, but doesn't know the node number of it.

One way to solve this could be to create a view that uses arguments and lists that authors nodes of that particular content type. That way you can inform the author they have reached the limit and at the same time present them with their node(s) and an option to quickly be able to edit any of them.

Re: Limit Node Creation with Node Limit Number and Rules in ...

thanks for your reply, using views is ok, and if I got it right what are you saying is that when a user logs in or visit his profile, the view with created nodes will be shown.

correct me if i'm wrong, this means that there will be no administration menu for this user - no "create new content" link ?!?
i have to solve the problem with users which will try to add new content by manualy entering the url for "create new content" - i will need rules again to do something like "delete node after node saved if other node/s exist"

once again thank you

Re: Limit Node Creation with Node Limit Number and Rules in ...

Yes, the view will be used to show the author the node(s) they have created earlier and thus be able to quickly edit them if needed.

It wont replace the create content link since it will be used by your node limit rule when a user has reached the limit.

You can of course expand on this and create a sort of content dashboard where each user can manage their own content easily.