Prevent the user from creating more than one node (of a certain type)

Avatar-eric-london
Created by Eric.London on 2009-08-30
Tags:
New Comment
 
Last year I wrote a quick code snippet to Prevent the user from creating more than one node of a certain type for Drupal 5. I received a comment request to update this code for Drupal 6.

<?php
function MYMODULE_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  // define node type that a user will only be allowed to create one instance of 
  $singleNodeType = 'YOUR-NODE-TYPE';
  
  // test for node/add/NODETYPE page
  if ($node->type==$singleNodeType && $op=='prepare' && arg(0)=='node' && arg(1)=='add') {

    // define sql to create node table
    $sql = "select nid from {node} where type='%s' and uid='%d'";
    
    // execute sql
    $resource = db_query($sql, $singleNodeType, db_escape_string($GLOBALS['user']->uid));
    $result = db_result($resource);

    // test for result
    if (!empty($result)) {
      
      // set a message
      drupal_set_message("Sorry, you are only allow to create one $singleNodeType.");
      
      // redirect the user
      drupal_goto('node/add');
      
    }
        
  }
  
}
?>


The above code tests if the user is on the node/add/NODETYPE page and if the user has already created an instance of the node type, sets a message and redirects them away from the node/add page.

Comments

 
  • great! but
    Created by Anonymous on 2010-03-11
    Hi Eric! Awesome: this solves a lot of problems; but...

    what about if an administrator wants to create different events cased on a "webform content type"?
    I guess that a user could apply for one event but not for another one based on the same content type? Is that so?

    Thank you so much for your help!
    Lorenzo