Hi everyone,

At the date of this post, there are no node limiting modules (limiting the amount of nodes a user can produce) that are in a final, non-beta version.

See "Comparison of Node Limiter type modules" at http://groups.drupal.org/node/16637.

Out of the five modules listed, only one module is recommended for D6, but it is in beta.

Therefore I decided to try and write my own module. I had never coded in Drupal before. Being a newbie to Drupal, I was intimidated by writing my own module, but I found it surprisingly simple.

The API documentation in Drupal is too complicated for me. I wrote this code by looking at the code of similar modules.

If anyone has any suggestions for improvement, in terms of security, stability or error handling, etc. please let me know. I thought I would share it to show how simple it is to write your own (if you need something simple) and in the hope someone else may be able to use it.

For novice coders: This code is for a single content type called "listing". Just replace listing with your content type name. And I listed the file names and their contents and where the files need to go.

Content of nodelimiter.module file:

<?php
// $Id$

function nodelimiter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
  {
    //When user clicks Add listing, $op will = "prepare" and $node->type = "listing"
    
    //If a new node is being prepared (opened) and node type is listing:
    if ($op=="prepare" && $node->type=="listing")
      {
        global $user;  // Needed for $user->uid to work (e.g. in query)
        
        // Query the node table to get the number of listings for the current user:
        $result = db_query("SELECT nid FROM {node} WHERE uid = %d AND type = '%s'", $user->uid, 'listing');
        
        //The number of listings is in db_affected_rows():
        if (db_affected_rows() >3)
          {
              drupal_set_message(t('There is a limit of 3 listings. To add a new listing, please delete an existing listing first.'));
              drupal_goto('mylistings'); 
              
          } // End of if
                
      } // End of if
      
  } // End of function

Content of nodelimiter.info file:

; $Id$
name = Nodelimiter
description = My node limiting module rev 01.001 - for listing content type only
core = 6.x
version = "6.x-01.001"

Just put both files in a directory called nodelimiter in your modules directory and you're cooking with gas.

For anyone providing comments on my code, thank you in advance for your time and effort.

Comments

kaakuu’s picture

This seems interesting. Not yet tested. Marking so that I can test.
Keep up the good work.

drupaloo-1’s picture

Thank you kaakuu.

Please let me know any results from your testing. Being new to this I would appreciate you letting me know how you would test this.

00110000’s picture

How this turn out? I'm currently looking for a way to set a limit for specific node types for different roles. User quota had potential, but is only limited to use by user basis, which would be nearly impossible to keep track of everyone registering on the site, thus a role user limit is needed.

I'm currently delving into the Rules module, but that doesn't seem to allow you to set a certain limit other than one node type. Unless, I'm completely clueless on how to use it.

Any help anyone?

jdwfly’s picture

This won't be very flexible, but if you want a flexible solution check out Node Limit Number.

The Node Limit Number project has been resurrected and many new features have been added. This module now integrates with Rules and the limits are very easy to create. Head over to the Node Limit Number project and grab the latest development copy. The 2.0 release will be out soon.