background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Create CCK Node Form in a Collapsible Fieldset

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;
}
?>