0

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
    node_save($node);
}

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:

$value['field_publish_date']

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

  • what is your Drupal version ? – Refineo Oct 29 '12 at 15:24
  • Drupal version is 7 – littledynamo Oct 29 '12 at 15:27
1

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

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

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

swicth($field_map['type'][$value['type']]){
  case 'story': //populate fields of story type;
    break;
  case 'page': //populate fields of page type;
    break;
}

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 .

| improve this answer | |
  • 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. – littledynamo Oct 29 '12 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? – littledynamo Oct 29 '12 at 16:07
0

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'];
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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