Drupal - Best practice for language key for "und" in hook_form_alter

Unfortunately, your self-answer is not so universal. Field language and node language are not the same things.
Field language is needed only if you are using Field Translation API (for example Entity translation). When you start using it, some fields are converted to translatable.

Translatable fields have language code, which really makes sense. But if the field is not translatable, the language code will always be (and for all content types, entities, etc) — LANGUAGE_NONE (best to use the constant LANGUAGE_NONE and not the string 'und').
So with your solution you can get a situation when a node is Russian, but a field of the node uses LANGUAGE_NONE.
Happily, fields are attached to the form with '#language' key, which always contains the correct language code.

$lang = $form['FIELDNAME']['#language'];
$form['FIELDNAME'][$lang][0]['value']['#value'] = $value;

If you have no access to attached form, Field Language API functions can be useful.


If you know the entity type and bundle use something like the following code, which calls field_language().

$field_language = field_language('node', $node, 'field_charity_author');  
$form['field_charity_author'][$field_language][0]['value']['#value'] = arg(3);

It will figure out the right language, and will work in multilingual sites.

Also have a look at field_get_items().


The whole und deal is one of the more annoying things in Drupal 7 that didn't work as well in practice as when it was designed.

Anyways you can get the first language using array_keys like this:

function my_module_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == "someID") {
      $langs = array_keys($form['field_charity_author']);
      $form['field_charity_author'][$langs[0]][0]['value']['#value'] = arg(3);
  }
}

That should work all the time.