Main menu

Drupal 7: restrict terms select list (on node form) to display terms only in current language

Published by dnovikov on Mon, 02/06/2012 - 09:52

Code snippet.

<?php
/**
 * Implements hook_form_alter()
 * Restricts terms select list to display terms only
 * in current language
 */
function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
 
$fields = array(
   
'CONTENTTYPE_node_form' => 'CONTENT_FIELD',
    [...]
  );
  if (isset(
$fields[$form_id])) {
    global
$language;
   
$field_name = $fields[$form_id];
   
$opts = $form[$field_name][LANGUAGE_NONE]['#options'];
   
$field = field_info_field($field_name);
    if (
$vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary'])) {
     
$query = new EntityFieldQuery();
     
$query->entityCondition('entity_type', 'taxonomy_term')
        ->
propertyCondition('vid', $vocabulary->vid)
        ->
propertyCondition('language', $language->language);
      if (
$result = $query->execute()) {
       
$result = array('_none' => '') + $result['taxonomy_term'];
       
$form[$field_name][LANGUAGE_NONE]['#options'] = array_intersect_key($opts, $result);
      }
    }
  }
}
?>

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.