My question is exactly similar to the one posted here https://www.drupal.org/node/2324047.

The answer posted here is not complete and I'm looking for a solution. I'm using the below code to save a node.

function api_add_getabstract($node_values, $api_endpoint) {
  //initialise node object
  $ex_node = new stdClass();
  $ex_node->type = 'getabstract';
  $ex_node->language = 'en';
  dpm($node_values,'node values');

  //add field values
  $ex_node->title = $node_values['bookinfo']['title'];
  $ex_node->body['en'][0]['value'] = $node_values['recommendation'];
  $ex_node->body['en'][0]['format'] = 'full_html';
  $ex_node->field_authors['en'][0]['value'] = $node_values['bookinfo']['authors'];
  $ex_node->field_sub_title['en'][0]['value'] = $node_values['bookinfo']['subtitle'];
  $ex_node->field_publisher['en'][0]['value'] = $node_values['bookinfo']['publisher'];
  $ex_node->field_ex_cover_image['en'][0]['value'] = $node_values['bookinfo']['bookcoverOriginal'];
  $ex_node->field_ex_deep_link['en'][0]['value'] = $node_values['publicURL'];
  $ex_node->field_api_info['en'][0]['value'] = $api_endpoint;

I tried by changing the value 'en' to 'und' and LANGUAGE_NONE, but none seem to save the node completely. Using 'en' at least creates the node with only Title field, but for the other 2 values, node is not even getting created.

  • And what happens when you completely forego setting a language at all? – leymannx Apr 20 '17 at 19:41
  • $ex_node->language can be set to LANGUAGE_NONE, every reference to lang codes after that should just be $ex_node->language to make change easier. Are you sure all of those fields data column is value as well? Not all Drupal fields are. – Kevin Apr 20 '17 at 19:54
  • Also: api.drupal.org/comment/53758#comment-53758 – Kevin Apr 20 '17 at 19:56
  • @leymannx: if I don't set $ex_node->language = 'en'; but use ['en'], node is getting saved with only Title field. Language shows as Undefined language (). – NKP Apr 20 '17 at 20:05
  • @Kevin: I was initially doing the same way you're mentioning. But LANGUAGE_NONE just doesn't seem to work. Also, I agree with you on the "value" thing, but at least "body" should be saved – NKP Apr 20 '17 at 20:07

I presume by your code example you use D7. And I presume by the error you get you don't use Entity Translation.

In D7 the built-in approach to offer multilingual content is called 'content translation'. Meaning you translate node by node. You hit the language switch and get redirected to another node.

Whereas in D8 the Entity Translation module now became part of core and so it replaced the 'content translation' approach by the 'entity translation' approach. Meaning you translate field by field. By providing a language key for every field. You hit the language switch and the same node reloads, but this time rendering the field values in a different language.

To come back to your question, as you don't use Entity Translation, you only have to set a language for the node (and possibly map it to their matching nodes in other languages). You MUST NOT specify a language for each field.

Alternatively, start using Entity Translation and configure the fields you want to be multilingual. Then, if I remember it right, your code will work.

Comparing both approaches, each has its pros and cons. And I already built D8 sites where we had to somehow rebuild a custom 'content translation' workflow (node by node translation), to get back the flexibility this has (especially in connection with Paragraphs – but that's a different story to be told).

  • Thanks for the detailed post, much appreciated. Now if I understand your answer correctly, I've replaced $ex_node->language = 'en'; with $ex_node->language = LANGUAGE_NONE; and all occurrences of ['en'] with simply []. This does save the node with language as Language neutral, but all other fields still are not getting any values – NKP Apr 21 '17 at 7:09
  • @NKP – No, you leave $node->language = 'en' and set all other occurences to LANGUAGE_NONE. – leymannx Apr 21 '17 at 7:19
  • @NKP – Please have a look at drupal.stackexchange.com/a/36960/15055 – leymannx Apr 21 '17 at 7:21
  • It seems the $node_values array was the culprit here and not LANGUAGE_NONE as I was assuming. I know this is embarrassing. Thanks for all the help – NKP Apr 21 '17 at 9:19

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