You are here

function i18ntaxonomy_form_alter in Internationalization 5

Same name and namespace in other branches
  1. 5.3 contrib/i18ntaxonomy.module \i18ntaxonomy_form_alter()
  2. 5.2 contrib/i18ntaxonomy.module \i18ntaxonomy_form_alter()
  3. 6 i18ntaxonomy/i18ntaxonomy.module \i18ntaxonomy_form_alter()

Implementation of hook_form_alter().

File

contrib/i18ntaxonomy.module, line 17
Internationalization (i18n) package - taxonomy term translation

Code

function i18ntaxonomy_form_alter($form_id, &$form) {
  if ($form_id == 'translation_admin_settings') {

    // Translation settings
    $translate = variable_get('i18ntaxonomy_vocabularies', array());
    $form['i18ntaxonomy_vocabularies'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#title' => t('Vocabulary Translation'),
      '#tree' => TRUE,
      '#weight' => 0,
      '#description' => t('Vocabularies to translate through localization system'),
    );
    foreach (taxonomy_get_vocabularies() as $voc) {
      $form['i18ntaxonomy_vocabularies'][$voc->vid] = array(
        '#type' => 'checkbox',
        '#title' => $voc->name,
        '#default_value' => $translate[$voc->vid],
      );
    }
  }
  elseif (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id && ($node = $form['#node']) && isset($form['taxonomy']) && ($translate = variable_get('i18ntaxonomy_vocabularies', array()))) {

    // Node form. Translate vocabularies
    if (!isset($node->taxonomy)) {
      if ($node->nid) {
        $terms = taxonomy_node_get_terms($node->nid);
      }
      else {
        $terms = array();
      }
    }
    else {
      $terms = $node->taxonomy;
    }

    // Regenerate the whole field for translatable vocabularies
    foreach (element_children($form['taxonomy']) as $vid) {
      if (is_numeric($vid) && $translate[$vid]) {

        // Rebuild this vocabulary's form
        $vocabulary = taxonomy_get_vocabulary($vid);

        // Extract terms belonging to the vocabulary in question.
        $default_terms = array();
        foreach ($terms as $term) {
          if ($term->vid == $vid) {
            $default_terms[$term->tid] = $term;
          }
        }
        $form['taxonomy'][$vid] = i18ntaxonomy_form($vocabulary->vid, array_keys($default_terms));
        $form['taxonomy'][$vid]['#weight'] = $vocabulary->weight;
        $form['taxonomy'][$vid]['#required'] = $vocabulary->required;
      }
    }
  }
  else {
    if ($form_id == 'views_filters' && ($translate = variable_get('i18ntaxonomy_vocabularies', array()))) {

      // We only translate exposed filters here
      $view = $form['view']['#value'];
      if ($view->exposed_filter) {
        foreach ($view->exposed_filter as $index => $filter) {
          $matches = array();
          if ($filter['field'] == 'term_node.tid') {

            // That's a full taxonomy box. Translate options: arary(tid => "Vocabulary: Term")
            // First, we get a translated list. Then we replace on the options array
            $replace = _i18ntaxonomy_vocabulary_terms(array_keys($translate));
            foreach ($replace as $tid => $name) {
              if (isset($form["filter{$index}"]['#options'][$tid])) {
                $form["filter{$index}"]['#options'][$tid] = $name;
              }
            }
          }
          elseif (preg_match("/term_node_(\\d+)\\.tid/", $filter['field'], $matches)) {
            $vid = $matches[1];
            if ($translate[$vid]) {

              // Translate this vocabulary terms, field name is filter$index vid = $matches[1]
              foreach ($form["filter{$index}"]['#options'] as $value => $option) {
                if ($value != '**ALL**') {

                  // ALL option should be already localized
                  // This may be an object with an option property being an array (tid => name)
                  if (is_object($option) && is_array($option->option)) {
                    foreach (array_keys($option->option) as $tid) {
                      $option->option[$tid] = t($option->option[$tid]);
                    }
                    $form["filter{$index}"]['#options'][$value] = $option;

                    // But it used to be a plain string, so let's keep this just in case...
                  }
                  elseif (is_string($option)) {
                    $form["filter{$index}"]['#options'][$value] = t($option);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}