0

Drupal 7

I'm creating a event type nodes, using data from an external database. It's a multi-lingual site, and the primary language is english.

My code creates the node correctly, when the language is set to LANGUAGE_NONE, but when it is set to "en", the body text is not saved in the database.

foreach ($results as $record) {
  echo '<br/>Creating event: ' . $record->name;

  $node = new stdClass();
  $node->type = 'event';
  $node->uid = 1;
  node_object_prepare($node);

  $node->title    = $record->name;
  $node->language = "en";
  //$node->language = LANGUAGE_NONE;

  $node->body[$node->language][0]['value'] = 'This is a body text for '.$record->name;
  $node->body[$node->language][0]['summary'] = 'Here goes a summary for '.$record->name;
  $node->body[$node->language][0]['format'] = 'raw';

  $path = 'content/programmatically_created_node_' . date('YmdHis');
  $node->path = array('alias' => $path);

  $instance = field_info_instance('node', 'body', 'event');
  print_r($instance);

  $node = node_submit($node);
  node_save($node);  
  dpm($node);
}

When $node->language = LANGUAGE_NONE, a record is created in the fields_data_body table. I can see the body text, but cannot translate it. I guess this is correct.

But when $node->language = "en", no record is created. This is my problem. I need this set to "en" as it's a multi-lingual site, and without it, there is no "translate" tab when editing the node (Internationalization module drupal.org/project/i18n)

There is some back history to this issue here.. No "body" when creating a node programatically, with Internationalization ..but it's not essential to read it.

2 Answers 2

0

It should be showing 'und' for language neutral nodes. Have you checked the value of the $node->language parameter?

You could also try creating one of these nodes with the user interface. Then install the devel module and use the php executor (whatever.com/devel/php) to load the node and output with dpm() to see the contents of the node. You probably have something wrong with the structure.

Example (after the sample node is created):

$node = node_load(21);
dpm($node);

That will output the node contents for you to help you better explore what you are creating.

2
  • Thanks for trying to help. I'd forgotten about this problem. The only solution (i.e. hack) I found was to use node_save(), then re-load the node, set the language to "en", and then node_save() again. I think there maybe a bug in core or i18n, but it's beyond me to fix it.
    – Rob
    Commented Jan 29, 2013 at 11:46
  • I had a similar problem that was caused by a data migration. I found that I just needed to do a node_load(), copy the body field to the proper language (from $node->body['en'] to $node-body['und']) and set the old language field to NULL. Then run it through node_save().
    – Patrick
    Commented Jan 29, 2013 at 16:36
0

The only solution I found was to update the language AFTER it had been saved.

$node = node_submit($node);
node_save($node);  

// This is a hack, until a good solution is found.
// Can't successfully set language on create, so update it afterwards
$node = node_load($node->nid);
$node->language = "en";
node_save($node);  

I know it's a hack. But it will do until a solution is found, or a bug fixed.

1

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.