i'm trying to create a multilingual node using a php script.

I'm doing this:

 $node = new stdClass();
 $node->title = "Test ".mktime();
 $node->type = "job";
 $node->language = 'de'; // Or e.g. 'en' if locale is enabled
 $node->uid = $user->uid; 
 $node->status = 1; //(1 or 0): published or not
 $node->promote = 0; //(1 or 0): promoted to front page
 $node->comment = 1; //2 = comments on, 1 = comments off
 $node->path = array('alias' => 'das-ist-ein-test-'.mktime());
 $node->body[$node->language][0]['value']   = "Body Body Body";
 $node->body[$node->language] = text_summary("Body Body Body");
 $node->body[$node->language]  = 'filtered_html';

As long as i set $node->language to 'und' everything is working fine. But as soon as I set the language to 'de' or 'en' (both languages are activated in my Drupal installation) the body text doesn't get saved.

So my question is: can anybody provide me with a working example of how to store a multlingual node (de,en) into Drupal using program code.

Thank you


It depends on is your body field translatable or not. Please, look at this explanation about translatable fields: https://drupal.stackexchange.com/a/31639/2466

$node_lang = 'ru'; // I am a Russian girl

$node = new stdClass();
$node->title = "Test ".mktime();
$node->type = 'article'; // Your type
$node->language = $node_lang;
$node->uid = 1; 
$node->status = 1;

// Get proper langcode
$body_field_info = field_info_field('body');
if (field_is_translatable('node', $body_field_info)) {
  $body_language = field_valid_language($node_lang);
} else {
  $body_language = LANGUAGE_NONE;

// Some lorem ipsum.
// You can add custom summary:
// $node->body[$body_language][0]['summary'] = 'Summary';
$node->body[$body_language][0]['value']   = 'Lorem monotonectally iterate resource-leveling innovation before timely core competencies. Globally coordinate sustainable strategic theme areas and intermandated infomediaries. Monotonectally brand customer directed solutions and high-quality bandwidth.';


There is an issue for Drupal 8 to make Field Language API more usable.

  • Thank you very much for your answer. It's working now. Alltough I find it a rather strange concept, that the node itself is languagespecific whereas the fields from this node are 'undefined'. I'm wondering if there is any use case where the fields are language specific (in my tests every field was 'und'). If the fields are always 'und' i could ditch the function call alltogether.
    – caspermc
    Jul 16 '12 at 8:30
  • 1
    It is not recommended to use just 'und' as field key in multilingual sites.
    – kalabro
    Jul 16 '12 at 8:34
  • @kalabro – Right, it's LANGUAGE_NONE instead.
    – leymannx
    Apr 21 '17 at 7:24

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.