I am working on a new module (uscongress module). The basics of the module is that it imports data and makes it available via CCK nodes. The dataset is a common one (U.S. Congressional Bills) that many people might want to develop applications around. I decided to try to implement the data using CCK rather than as a custom data model, thinking that CCK nodes would be more flexible to application developers.

It has posed three important development hurdles.

Create Content Types

Export the content type from CCK (admin/content/types/export), save the array in an array, and then pass it to the function below. This improves upon the previously known techniques, in that the exported CCK types no longer need to be escaped.

function _install_create_content($content) {
  $type = $content['type']['type'];
  global $_install_macro;
  $_install_macro[$type] = $content;
  include_once './'. drupal_get_path('module', 'node') .'/content_types.inc';
  include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');
  $macro = 'global $_install_macro; $content = $_install_macro['. $type .'];';
    array('type_name' => '', 'macro' => $macro));

Inserting Nodes

See Quick and Dirty CCK Imports.

The correct way to insert and update nodes is with drupal_execute, rather than node_submit because it handles form alter and form validation.

Debug the node edit form by placing a print_r($node) in node.module at the top of node_submit, edit node/add/yourtype, submit the form, remove your debugging print_r, then create your values array to match these values.

$node->type = 'yourtype';
$values = array();
$values[...] = ...;
drupal_execute('yourtype_node_form', $values, $node);
$errors = form_get_errors();
if (count($errors)) {
  // do something ...

Updating Nodes

When updating nodes, you'll need to load the node using node_load, but then before updating the values, the default values need to be pre-populated from the node.

$node = node_load($nid);
_content_widget_invoke('prepare form values', $node);


Don't assume the database tables and columns are, even if they are created in by your module. As CCK fields, the administrator can add them to other tables, in which case, the single instance fields become multiple instance fields, and they database references change.

Always use content_database_info to get the database info. For example:

  $field1 = content_database_info(content_fields('yourfield1', 'yourtable'));
  $table1 = $field1['table'];
  $column1 = $field1['columns']['value']['column'];

  $field2 = content_database_info(content_fields('yourfield2', 'yourtable'));
  $table2 = $field2['table'];
  $column2 = $field2['columns']['value']['column'];

  if ($table1 == $table2) {
    $sql = "SELECT n.*, t1.$column1, t1.$column2 FROM {node} n ";
    $sql .= " INNER JOIN {$table1} t1 ON t1.nid = n.nid AND t1.vid = n.vid";
  else {
    $sql = "SELECT n.*, t1.$column1, t2.$column2 FROM {node} n ";
    $sql .= " INNER JOIN {$table1} t1 ON t1.nid = n.nid AND t1.vid = n.vid";
    $sql .= " INNER JOIN {$table2} t2 ON t2.nid = n.nid AND t2.vid = n.vid";
  $sql .= " WHERE n.nid = %d";