I'm pulling content from a web service and adding the nodes programmatically. Here is my code so far:

foreach($content as $key => $value) {
    $node = new stdClass();
    $node->title =  $value['title'];
    $node->type = $field_map['type'][$value['type']];   // Use remote type as key in field map array to get local type
    node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
    $node->language = (isset($value['language'])) ? $value['language'] : LANGUAGE_NONE;
    $node->uid = $GLOBALS["user"]->uid;
    $node->status = $value['status']; //(1 or 0): published or not
    $node->promote = $value['promote']; //(1 or 0): promoted to front page
    $node->comment = $value['comment']; //2 = comments on, 1 = comments off
    $node->created = $value['created'];
    $node->changed = $value['changed'];
    $node->sticky = $value['sticky'];
    $node->translate = $value['translate'];
    $node->uuid = $value['uuid'];

    $node = node_submit($node); // Prepare node for saving

The example I'm following says to add cck fields like this:

// Entity reference field
    $node->field_customer_nid[$node->language][] = array(
        'target_id' => $form_state['values']['entity id'],
        'target_type' => 'node',

I'm having difficulty because my CCK fields will vary depending on the content type that is being pulled form the web service. My fields are stored in the following format:


How could I go about adding the fields to my node?

  • what is your Drupal version ?
    – LLub
    Commented Oct 29, 2012 at 15:24
  • Drupal version is 7 Commented Oct 29, 2012 at 15:27

2 Answers 2


First I would recommend using this type of structure for crating nodes progmatically

$node = new stdClass();
$node->type = 'story';

If you're worried bout the content type being an issue use a switch statement by passing the content type in:

  case 'story': //populate fields of story type;
  case 'page': //populate fields of page type;

Also if you are using a web service to import nodes I would suggest that you use the Feeds module http://drupal.org/project/feeds rather than creating them manually. I'm speaking from experience .

  • Thanks for your answer. New content types will be added regularly, so I'd prefer to not have to amend the switch statement each time. I'm thinking I may have to create the node first and then add the CCK fields separately via sql. Commented Oct 29, 2012 at 15:34
  • Did you mean that I should use the feeds module to create the nodes? Or use the feeds module to create the whole import process? Commented Oct 29, 2012 at 16:07

Okay, figured this one out. If you don't know the name of the fields until runtime then you can assign the field name using curly braces when assigning to the stdClass e.g:

  $node->{$field_name}[$node->language][$j]['value'] = $field_value['value'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.