以编程方式创建多语言节点


8

我正在尝试使用php脚本创建一个多语言节点。

我正在这样做:

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

只要我将$ node-> language设置为“ und”,一切都可以正常工作。但是,一旦我将语言设置为“ de”或“ en”(在我的Drupal安装中两种语言都被激活),主体文本就不会保存。

所以我的问题是:有人能提供给我一个如何使用程序代码将多个节点(de,en)存储到Drupal的有效示例。

谢谢


[链接]我希望这可以帮助您在正确的方向(drupal.stackexchange.com/questions/6401/...
维克

Answers:


9

这取决于您的身体视野是否可翻译。请查看有关可翻译字段的以下说明: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.';

node_save($node);

还有一个问题,为Drupal 8进行实地语言API更可用。


非常感谢您的回答。现在正在工作。尽管我发现这是一个相当奇怪的概念,该节点本身是特定于语言的,而该节点中的字段是“未定义的”。我想知道字段是否特定于语言(在我的测试中,每个字段都是“ und”)。如果这些字段总是“ und”,我可以放弃所有的函数调用。
caspermc

1
不建议'und'在多语言站点中仅将其用作字段键。
kalabro 2012年

@kalabro –对,LANGUAGE_NONE相反。
leymannx
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.