Creating a node object

Creating a node object

Now moving on. First a function just to create the basic node object. Everything in this import is going into an Organic group, so there are a set of additional og_ related variables that are common to all the nodes imported into this site. Of course print_r or better still var_export are your friend.

/**
 * Basic node - using body but with standard extra fields
 *
 * @param $xc array row from xoops_import_* table
 * @param $type string drupal content type
 * @param $village object drupal village node (organic group)
 * @param $promote bool the only change to the common basic node here
 * @return $node object basic node
 */

function _x_node_create_basic($xc, $type, $village, $promote = 1) {
  $node = new stdClass();
  $node->type = $type;
  $node->status = 1;
  $node->promote = $promote;
  $node->sticky = 0;
  // the users are already migrated in,
  // nicely with this clean migration we are able to use the same uid
  // but there was one gotcha see note below
  $node->uid = ($xc['uid']) ? $xc['uid'] : $village->uid;
  $node->created = $xc['date_created'];
  // note that the updated date is set by Drupal within node_save
  $node->language = 'en';
  $node->title = $xc['title'];
  $node->body = $xc['content'];
  if ($xc['format'] == 'html') {
    // a special filtered, but more permissive 'imported html' filter has been added
    $node->format = 5;
  }
  else {
    $node->format = 0;
  }
  // the terms are going to be added to the object later,
  // but it wants to be an empty array no matter
  $node->taxonomy = array();

  // organic groups
  $node->og_initial_groups = array();
  $node->og_groups = array($village->nid);
  $node->og_public = ! $village->og_private;

  // common extra fields
  $node->field_embedded_video = _x_node_embedded($xc['content']);
  list($images, $attach) = _x_node_files($xc['content']);
  $node->field_picture = $images;
  $node->field_attachment = $attach;
  $node->field_external_link = _x_node_ext_links($xc['content']);
  foreach ($node->field_external_link as $ex_link) {
    // check to see if any of the links are embedded video
    if (count(emfield_parse_embed(content_fields('field_embedded_video'), $em_link, 'video_cck'))) {
      $node->field_embedded_video[]['embed'] = $em_link;
    }
  }

  // used for recording save
  $node->xc_xtable = $xc['source_table'];
  $node->xc_xid = $xc['source_id'];

  return $node;
}
After that there were fields that weren't common to the different content types. So for example:
function _x_node_create_background($xc, $village, $promote = 0) {
  $node = _x_node_create_basic($xc, 'village_background', $village, $promote);
  // This is one of those nodes that uses CCK text fields only
  // with no default Body, let Drupal sort any body
  $node->body='';
  list($teaser, $body) = _x_node_teaser($xc['content']);
  $node->field_teaser = array( array('value' => $teaser, 'format' => 1));
  $node->field_body = array( array('value' => $body, 'format' => 1));
  // nice one this xoops bb had attachments base_64 encoded
  if (isset($xc['base64_attach']) && $xc['base64_attach'] != '') {
    $b_attach = unserialize(base64_decode($xc['base64_attach']));
    foreach ($b_attach as $b_file) {
      $node->field_attachment[] = _x_node_create_file('uploads/issues/'. $b_file['name_saved'], false, $b_file['name_display']);
    }
  }


  return $node;
}

Notes

  • The content searching function for the CCK fields were expensive as far as I could tell. As many moved to where the are absolutely needed speeds things up
  • Despite the users uid's being nicely migrated for the same id numbers the previous system had been deleting users. Drupal does an INNER JOIN on the users table so if there uid isn't there the content disappears. These users content was set to uid 0 after the fact.