Snippet for removing language neutral from create node forms

Last modified: April 27, 2009 - 13:52

The following snippet goes in a custom module, where "custommodule" should be replaced with the name of your custom module:

<?php
function custommodule_form_alter(&$form, $form_state, $form_id) {
  if (isset(
$form['#id']) && $form['#id'] == 'node-form') {
   
$default = language_default();
    if (isset(
$form['#node']->type) && variable_get('language_content_type_'. $form['#node']->type, 0)) {
     
$form['language'] = array(
       
'#type' => 'select',
       
'#title' => t('Language'),
       
'#default_value' => (isset($form['#node']->language) ? $form['#node']->language : $default->language),
       
'#options' => locale_language_list('name'),
      );
    }
  }
}
?>

it basically removes the language neutral options and sets the default to the default language (the one marked as default in the languages settings page)

NOTE: this assumes that nothing else has messed with the $form['language']

Slight revision

nwe_44 - September 14, 2009 - 20:47

This version respects whether the user is inputting a translation or not

<?php
function custommodule_form_alter(&$form, $form_state, $form_id) {


/* This removes the language neutral option from the node creation form, and resets it to a default. */

 
if (isset($form['#id']) && $form['#id'] == 'node-form') {

    if (isset(
$form['#node']->type) && variable_get('language_content_type_'. $form['#node']->type, 0)) {
   
$node = $form['#node'];
    if (!empty(
$node->translation_source)) {
     
// We are creating a translation. Add values and lock language field.
     
$form['translation_source'] = array('#type' => 'value', '#value' => $node->translation_source);
     
$form['language']['#disabled'] = TRUE;
    }else{
        global
$language;  
       
$form['language'] = array(
       
'#type' => 'select',
       
'#title' => t('Language'),
       
'#default_value' => (isset($form['#node']->language) ? $form['#node']->language : $language->language),
       
'#options' => locale_language_list('name'),
      );
    }
    }
  }
}
?>

If it doesn't work, you might need to change the custom module's weight, by doing something like

<?php
/**
* Implementation of hook_install().
*/
function CUSTOM_MODULE_NAME_install() {
 
 
// sink module's weight to the deepest depths of the module_list()
 
db_query("UPDATE {system} SET weight = 998 WHERE name = '[YOUR CUSTOM MODULE NAME]'");

}


?>

in the module's .install file.

 
 

Drupal is a registered trademark of Dries Buytaert.