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.
Add new comment