2

I'm trying to manually update a node and I'm not sure how the language works.

The node is in English, so $node->language = 'en';

However, all my fields data are organized like this in the node:

$node->field_name['und'][0]['value']

I thought 'und' means "language undefined", so shouldn't it be 'en' instead?

I'm trying to automate the process so is it safe to assume fields will always use 'und' in nodes?

0

3 Answers 3

4

Yes - it is safe to assume this. The locale module will deal with this by setting it to the language code of the default language, ex 'en'.

Citing from: http://drupal.org/update/modules/6/7#drupal_language_none

In Drupal 7, the LANGUAGE_NONE constant should be used for nodes or other items that do not have a specific language associated with it.

http://api.drupal.org/api/drupal/includes--bootstrap.inc/constant/LANGUAGE_NONE/7
0

You can get field values without use LANGUAGE_NONE.

$field_items = field_get_items('node', $node, 'field_name');
$value = $field_items[0]['value'];
-1

Instead of specifying a language, I prefer to just use entity_metadata_wrapper().

For example:

$node = entity_metadata_wrapper('node', SOME_NODE_ID);
$value = $node->field_something->value(); // returns field value
$node->field_something->set('new value'); // sets field value
$node->save();

Source: https://www.drupal.org/docs/7/api/entity-api/entity-metadata-wrappers

2
  • Hey hey, welcome to DA. Nice snippet. But I don't actually see how this answers OP's problem?
    – leymannx
    Jun 14, 2018 at 21:40
  • It's more of a different way to solve the root problem so that the actual question becomes a moot point.
    – Garrett W.
    Jun 14, 2018 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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