I'm creating a site for a car dealership and needed a form to add vehicles to the inventory. I have created a module with my custom form in it that collects all the information I need. I had to make a custom form because I needed to code in a hierachical select for make, then corresponding models and that is not supported with CCK (there is also a module for making hierarchical selects, but it hasn't been brought over yet to Drupal 6). I also have created a corresponding content type in CCK which matches the fields I defined in the custom form. Basically, I want to have my custom form submit all the information it's collected to the vehicle content type so that I will be able to use views on the inventory of vehicles.

I'm pretty sure that I need to do something in this part of the form:

function add_inventory_form_submit($form, &$form_state) {
  // code that submits to CCK content type
  drupal_set_message(t('This vehicle has been added to the inventory.'));
}

I also think that I may need to do something with drupal_execute, and/or something with exporting the CCK content type and using the code I get from that, but I'm stuck on what do do next (or even if that's how I should be going about this).

I've searched drupal.org and google, but I haven't been able to find anything that will help me put all the pieces together. Does anyone have any resources or examples that I could look at that may give me an idea of how to accomplish this? I'd appreciate any kind of help at all.

Comments

ocelotspot86’s picture

Ok so I think what I'm looking for is something like this:

function add_inventory_form_submit($form, &$form_state) {

    // 'vehicle' is the name of the content type I want the form to 
    // submit to. I have already created a new content type called 
    // 'vehicle' with CCK and defined all the fields from my custom 
    // form. I'm going to use $node later as an argument in drupal_execute
    $node = array('type' => 'vehicle'); 

    // This includes the module that defines node_form. I get errors 
    // when I don't require this module and use node_form.  
    // I got this tip from http://drupal.org/node/201739
    require_once drupal_get_path('module', 'node') . ('/node.pages.inc');

    // This is what I have gathered so far from 
    // http://api.drupal.org/api/function/drupal_execute/6). I think 
    // I need to use drupal_execute to make the form. The 
    // first argument is supposed to either be a unique ID for a form, 
    // or a function that builds an array to insert your form values. I 
    // put vehicle_node_form as the first argument. I think node_form  
    // does some sort of form-node translation for content type 
    // 'vehicle' and outputs an array. The second argument is 
    // $form_state(the values from the form), and I think you 
    // need $node in order for  node_form work.
    drupal_execute('vehicle_node_form', $form_state, $node);

    drupal_set_message(t('This vehicle has been added to the inventory.'));
}

It's not working for me yet, However, I've been messing around with the database I'm pretty sure I've messed something up. I'm going to bed and I'll create a fresh database to test this tomorrow.

gbrussel’s picture

This is something similar to what I tackled recently using a Webform submission to generate a CCK node.

require 'modules/node/node.pages.inc';
// Include the required stuff and then generate the new instance of a node object.  Make sure it's not named $node, or you'll break stuff.
$new = new stdClass();
// Give it a type equal to the machine-readable name of your CCK content type.
$new->type = 'shared';
// Prepares the entire node object, and then we'll manually override stuff we want.  Anything not overridden stays default.
node_object_prepare($new);
if (isset($form_values['submitted_tree']['image'])) {
  $image = unserialize($form_values['submitted_tree']['image']);
  $new->field_image = array(
    array(
      'fid' => $image['fid'],
      'title' =>$image['filename'],
      'filename' => $image['filename'],
      'filepath' => $image['filepath'],
      'filesize' => $image['filesize'],
    ),
  );
}
$new->uid = 0;
$new->status = 0;
$new->moderate = 1;
$new->title = $form_values['submitted_tree']['title'];
$new->field_screenname[0]['value'] = $form_values['submitted_tree']['screenname'];
$new->field_story[0]['value'] = $form_values['submitted_tree']['story'];
// Once all fields have been set, save the node.
node_save($new);

Note that I did a bit of tricky stuff to save the image that was on the form.

ocelotspot86’s picture

In case anyone was following this, I wasn't able to get the form to submit to a node the way I wanted. I just couldn't find documentation or relevant examples that I could understand. I ended up finding a way to get Conditional Fields to work, solving my original problem, and now I add inventory to the site using the CCK.