background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

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.

Eric.London's picture

Recently, I was using a REST implementation to fetch images from another server. Originally, I wrote the code to save the retrieved images as attachments to a single node. I revised the code to create custom separate custom CCK nodes using the imagefield module. Here's a code snippet that programmatically creates the new nodes...

<?php
$node
= new StdClass();
$node->type = 'MYNODETYPE';
$node->title = 'MYNODETITLE';
$file_temp = file_get_contents($tempFile);
$file_temp = file_save_data($file_temp, file_directory_path() .'/' . $fileName, FILE_EXISTS_RENAME);
$node->field_MYIMAGEFIELD = array(
  array(
   
'fid' => 'upload',
   
'title' => basename($file_temp),
   
'filename' => basename($file_temp),
   
'filepath' => $file_temp,
   
'filesize' => filesize($file_temp),
   
'filemime' => mime_content_type($file_temp),
  ),
);
$node->uid = $GLOBALS['user']->uid;
$node->status = 1;
$node->name = $GLOBALS['user']->name;

// save node
node_save($node);
?>

Eric.London's picture

In certain situations you may want to prevent the user from creating more than one node (of a certain type). This can be accomplished using the hook_nodeapi...

<?php
function MYMODULE_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
 
// ...some code...
 
if ($node->type=='YOURNODETYPE' && 'op'=='prepare' && $_REQUEST['q']=='node/add/YOURNODETYPE') {
   
$sql = "select nid from {node} where type='YOURNODETYPE' and uid='%d' and status='1'";
   
$resource = db_query($sql, db_escape_string($GLOBALS['user']->uid));
   
$result = db_result($resource);
    if (
$result) drupal_goto('SOMELOCATION');
  }
 
// ...some code...
?>

NOTE: the above code will prevent the user from adding another node by redirecting them away from the node/add page

Eric.London's picture
NOTE: this page has been replaced.

I wanted to embed a form in the page of a custom module to create a CCK node type. I thought it would be neat to enclose the form in a collapsible fieldset...

<?php
function MODULENAME_SOMEFUNCTION() {
 
$page_contents = "";
 
//...code...
 
$page_contents .= drupal_get_form('_MODULENAME_cck_form', 'MY_CCK_NODE_TYPE');
 
//...code...
 
return $page_contents;
}

function
_MODULENAME_cck_form($nodeType) {
 
$form = array();
  
 
// get a list of node types
 
$nodeTypes = node_get_types();
  
 
// create empty node
 
$node = array(
   
'type'    => $nodeType,
   
'uid'      => $GLOBALS['user']->uid,
   
'name'    => $GLOBALS['user']->name,
  );
  
 
// create the cck form fields
 
$form = content_form($node);

 
// get a list of node forms
 
$nodeForms = node_forms();
  
 
// get the callback for this node type
 
$nodeFormCallback = $nodeForms[$nodeType.'_node_form']['callback'];
  
 
// get the node form fields
 
$form = array_merge($form, node_form($node));
  
 
// add fieldset
 
$fieldset = array();
 
$fieldset[$nodeType] = array(
   
'#type' => 'fieldset',
   
'#title' => 'Add ' . $nodeTypes[$nodeType]->name,
   
'#collapsible' => TRUE,
   
'#collapsed' => TRUE,
  );
  
 
// add form to fieldset
 
foreach ($form as $k => $v) {
   
$fieldset[$nodeType][$k] = $v;
  }
  
  return
$fieldset;
}
?>

Eric.London's picture

Here is a script I wrote to generate passwords...

<?php
function passwordCreate($length = 8) {
  if (
$length>62 || $length<1) $length = 8;
 
$chars = array();
  for (
$i=0; $i<$length; $i++) {
    do {
     
$r = rand(48, 122);
    }
    while ((
$r>57 && $r<65) || ($r>90 && $r<97) || in_array(chr($r),$chars));
     
$chars[] = chr($r);
  }   
  return
implode('', $chars);
}
?>

Syndicate content