You are here

function i18n_form_alter in Internationalization 5.2

Same name and namespace in other branches
  1. 5.3 i18n.module \i18n_form_alter()
  2. 5 i18n.module \i18n_form_alter()
  3. 6 i18n.module \i18n_form_alter()

Implementation of hook_form_alter

This is the place to add language fields to all forms

File

./i18n.module, line 798
Internationalization (i18n) module

Code

function i18n_form_alter($form_id, &$form) {

  //drupal_set_message("DEBUG: i18n_form_alter form_id=$form_id ");
  switch ($form_id) {
    case 'taxonomy_overview_vocabularies':
      $vocabularies = taxonomy_get_vocabularies();
      $languages = i18n_supported_languages();
      foreach ($vocabularies as $vocabulary) {
        if ($vocabulary->language) {
          $form[$vocabulary->vid]['type']['#value'] = $form[$vocabulary->vid]['type']['#value'] . '&nbsp(' . $languages[$vocabulary->language] . ')';
        }
      }
      break;
    case 'taxonomy_form_vocabulary':

      // Taxonomy vocabulary
      if (isset($form['vid'])) {
        $vocabulary = taxonomy_get_vocabulary($form['vid']['#value']);
      }
      $form['i18n'] = array(
        '#type' => 'fieldset',
        '#title' => t('Multilingual options'),
        '#collapsible' => TRUE,
        '#weight' => -20,
      );
      $form['i18n']['language'] = _i18n_language_select(isset($vocabulary) ? $vocabulary->language : i18n_get_lang(), t('This language will be set for all terms in this vocabulary'));
      break;
    case 'taxonomy_form_term':

      // Taxonomy term
      if (isset($form['tid']) && is_numeric($form['tid']['#value'])) {
        $term = taxonomy_get_term($form['tid']['#value']);
      }
      $form['language'] = _i18n_language_select(isset($term) ? $term->language : i18n_get_lang());
      break;
    case 'node_type_form':
      $node_type = $form['old_type']['#value'];

      // Build a fieldset to allow some more options here
      $form['workflow']['i18n'] = array(
        '#type' => 'fieldset',
        '#title' => t('Multilingual options'),
      );
      $form['workflow']['i18n']['i18n_node'] = array(
        '#type' => 'radios',
        '#title' => t('Multilingual content'),
        '#default_value' => variable_get('i18n_node_' . $node_type, 0),
        '#options' => _i18n_content_languages(),
        '#description' => t('Enables language field and translations for this content type.'),
      );
      break;
    case '_locale_admin_manage_screen':

      // Locale language settings
      $languages = locale_supported_languages(TRUE, TRUE);
      $i18nlangs = variable_get('i18n_languages', array());
      $form['native'] = array(
        '#tree' => TRUE,
      );
      foreach ($languages['name'] as $key => $lang) {
        $options[$key] = '';
        $form['native'][$key] = array(
          '#type' => 'textfield',
          '#default_value' => $i18nlangs['native'][$key] ? $i18nlangs['native'][$key] : $lang,
          '#size' => 15,
          '#maxlength' => 64,
        );
      }
      $form['rtl'] = array(
        '#type' => 'checkboxes',
        '#options' => $options,
        '#default_value' => $i18nlangs['rtl'],
      );
      $form['#base'] = 'i18n_admin_manage_screen';
      if (!is_array($form['#submit'])) {
        $form['#submit'] = array(
          '_locale_admin_manage_screen_submit' => array(),
        );
      }
      $form['#submit'] = array(
        'i18n_admin_manage_screen_submit' => array(),
      ) + $form['#submit'];
      break;
    case 'menu_edit_item_form':
      if ($mid = $form['mid']['#value']) {
        $language = db_result(db_query('SELECT language FROM {menu} WHERE mid = %d', $mid));
      }
      else {
        $language = '';
      }
      $form['language'] = _i18n_language_select($language, t('You can set a language for this menu item.'));
      $form['#submit'] = array(
        'i18n_menu_edit_item_form_submit' => array(),
      );
      break;
    default:

      // Node edit form
      if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id && ($node = $form['#node'])) {

        // Language field
        if (variable_get('i18n_node_' . $form['type']['#value'], 0) && !isset($form['i18n']['language'])) {

          // Language field
          $form['i18n'] = array(
            '#type' => 'fieldset',
            '#title' => t('Multilingual settings'),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
            '#weight' => -4,
          );

          // Language will default to current only when creating a node
          $language = isset($form['#node']->language) ? $form['#node']->language : (arg(1) == 'add' ? i18n_get_lang() : '');
          $form['i18n']['language'] = _i18n_language_select($language, t('If you change the Language, you must click on <i>Preview</i> to get the right Categories &amp; Terms for that language.'), -4, i18n_node_language_list($node));
          $form['i18n']['trid'] = array(
            '#type' => 'value',
            '#value' => $form['#node']->trid,
          );
        }

        // Correction for lang/node/nid aliases generated by path module
        // if($form['#node']->path && $form['#node']->path == i18n_get_lang().'/node/'.$form['#node']->nid){
        if ($node->path) {
          $alias = drupal_lookup_path('alias', 'node/' . $node->nid);
          if ($alias && $alias != 'node/' . $node->nid) {
            $form['#node']->path = $alias;
          }
          else {
            unset($form['#node']->path);
          }
        }
      }

      // Multilingual variables in settings form
      if ($form['#base'] == 'system_settings_form' && ($variables = variable_get('i18n_variables', 0))) {
        if (i18n_form_alter_settings($form, $variables)) {
          $form['#submit'] = array(
            'i18n_variable_form_submit' => array(),
          );
        }
      }
  }
}