All pastes #773046 Raw Edit

Stuff

public text v1 · immutable
·published 2007-11-14 06:30 UTC
rendered paste body
/**
 * Add node content.
 * This is proving to be fragile. (Surprise, surprise)
 *
 * @param string $content_type, should match existing content types
 * @param array $properties, with fields you need to explicitly set.
 *
 * Return a node.
 */
function install_create_content($content_type, $properties, $flag = FALSE) {
  global $user;

  $default = array();
  $default['type'] = $content_type;
  $default['format'] = 0;
  $default['comment'] = 2; // Enable read/write comments
  $default['status'] = 1;  // Published
  $default['promote'] = 0; // Not promoted to front page.
  $default['sticky'] = 0;  // Not sticky.

  $default['created'] = date('g:i:sA');
  $default['log'] = 'Updated at ' . date('g:i:sA') . ' via lpimport' ;

  $default['name'] = $user->name;
  $default['uid'] = $user->uid;

  foreach ($properties AS $property => $value) {
    $field = content_fields($property, $content_type);
    if (isset($field['field_name'])) {
      switch ($field['type']) {
        case 'nodereference':
        case 'userreference':
          $default[$property] = array(array('nid' => $value));
          break;
        default:
          if (is_array($value)) {
            // Uncertain.
            $default[$property][] = $value;
          }
          else {
            $default[$property] = array(array('value' => $value));
          }
      }
      if ($field['widget']['type'] == 'options_select' && isset($default[$property][0]['value'])) {
        if ($field['multiple']) {
          foreach ($property[0] AS $v) {
            $default[$property]['keys'][$value] = $value;
          }
        }
        else {
          $default[$property]['key'] = $default[$property][0]['value'];
        }
        //unset($default[$property][0]);
      }
      unset($properties[$property]);
    }
  }

  if($flag) {
    dpr($default); print "E"; ;
  }
  $node = array_merge($default, $properties);
  if($flag) {
    dpr($node); print "F";
  }
  $node = node_submit($node);
  if($flag) {
    dpr($node); print "G";
  }
  node_save($node);
  if($flag) {
    dpr($node); print "H";
  }
  return $node;
}