You are here

function i18n_taxonomy_form_taxonomy_form_term_alter in Internationalization 7

Implements hook_form_FORM_ID_alter()

File

i18n_taxonomy/i18n_taxonomy.module, line 746
i18n taxonomy module

Code

function i18n_taxonomy_form_taxonomy_form_term_alter(&$form, &$form_state) {

  // Check for confirmation forms
  if (isset($form_state['confirm_delete']) || isset($form_state['confirm_parents'])) {
    return;
  }
  $term = $form_state['term'];
  $vocabulary = $form['#vocabulary'];

  // Mark form so we can know later when saving the term it came from a taxonomy form
  $form['i18n_taxonomy_form'] = array(
    '#type' => 'value',
    '#value' => 1,
  );

  // Add language field or not depending on taxonomy mode.
  switch (i18n_taxonomy_vocabulary_mode($vocabulary->vid)) {
    case I18N_MODE_TRANSLATE:

      // Set $form_state['storage'] default as empty array because we will add
      // the translation and target from $_GET. So we still have it when the
      // page partially reloads with ajax.
      if (!isset($form_state['storage'])) {
        $form_state['storage'] = array();
      }

      // get translation from $_GET or $form_state['storage']
      $translation = null;
      if (isset($_GET['translation'])) {
        $translation = $_GET['translation'];
        $form_state['storage']['translation'] = $translation;
      }
      else {
        if (isset($form_state['storage']) && isset($form_state['storage']['translation'])) {
          $translation = $form_state['storage']['translation'];
        }
      }

      // get target from $_GET or $form_state['storage']
      $target = null;
      if (isset($_GET['target'])) {
        $target = $_GET['target'];
        $form_state['storage']['target'] = $target;
      }
      else {
        if (isset($form_state['storage']) && isset($form_state['storage']['target'])) {
          $target = $form_state['storage']['target'];
        }
      }
      $form['language'] = array(
        '#description' => t('This term belongs to a multilingual vocabulary. You can set a language for it.'),
      ) + i18n_element_language_select($term);

      // If the term to be added will be a translation of a source term,
      // set the default value of the option list to the target language and
      // create a form element for storing the translation set of the source term.
      if (empty($term->tid) && isset($translation) && isset($target) && ($source_term = taxonomy_term_load($translation)) && ($target_language = i18n_language_object($target))) {

        // Set context language to target language.
        i18n_language_context($target_language);

        // Add the translation set to the form so we know the new term
        // needs to be added to that set.
        if (!empty($source_term->i18n_tsid)) {
          $translation_set = i18n_taxonomy_translation_set_load($source_term->i18n_tsid);
        }
        else {

          // No translation set yet, build a new one with the source term.
          $translation_set = i18n_translation_set_create('taxonomy_term', $vocabulary->machine_name)
            ->add_item($source_term);
        }
        $form['language']['#default_value'] = $target_language->language;
        $form['language']['#disabled'] = TRUE;
        drupal_set_title(t('%language translation of term %title', array(
          '%language' => locale_language_name($_GET['target']),
          '%title' => $source_term->name,
        )), PASS_THROUGH);
      }
      elseif (!empty($term->tid) && i18n_object_langcode($term)) {

        // Set context language to term language.
        i18n_language_context(i18n_language_object($term->language));

        // If the current term is part of a translation set,
        // remove all other languages of the option list.
        if (!empty($term->i18n_tsid)) {
          $translation_set = i18n_taxonomy_translation_set_load($term->i18n_tsid);
          $translations = $translation_set
            ->get_translations();

          // If the number of translations equals 1, there's only a source translation.
          if (count($translations) > 1) {

            //unset($form['language']['#options'][LANGUAGE_NONE]);
            foreach ($translations as $langcode => $translation) {
              if ($translation->tid !== $term->tid) {
                unset($form['language']['#options'][$langcode]);
              }
            }
            $form['language']['#description'] = t('This term already belongs to a <a href="@term-translation">translation set</a>. Changing language to <i>Language neutral</i> will remove it from the set.', array(
              '@term-translation' => url('taxonomy/term/' . $term->tid . '/translate'),
            ));
          }
        }
      }

      // If we've got a translation set, add it to the form.
      if (!empty($translation_set)) {
        $form['translation_set'] = array(
          '#type' => 'value',
          '#value' => $translation_set,
        );
      }
      break;
    case I18N_MODE_LANGUAGE:

      // Set context language to vocabulary language and add value to the form.
      i18n_language_context(i18n_language_object($vocabulary->language));
      $form['language'] = array(
        '#type' => 'value',
        '#value' => $vocabulary->language,
      );
      $form['identification']['language_info'] = array(
        '#value' => t('All terms in this vocabulary have a fixed language: %language', array(
          '%language' => i18n_language_name($vocabulary->language),
        )),
      );
      break;
    case I18N_MODE_LOCALIZE:
      $form['language'] = array(
        '#type' => 'value',
        '#value' => LANGUAGE_NONE,
      );
      break;
    case I18N_MODE_NONE:
    default:
      $form['language'] = array(
        '#type' => 'value',
        '#value' => LANGUAGE_NONE,
      );
      break;
  }
  if (user_access('translate interface') && i18n_taxonomy_vocabulary_mode($vocabulary->vid) & I18N_MODE_MULTIPLE) {
    $form['actions']['translate'] = array(
      '#type' => 'submit',
      '#name' => 'save_translate',
      '#value' => t('Save and translate'),
      '#weight' => 5,
      '#states' => array(
        'invisible' => array(
          // Hide the button if term is language neutral.
          'select[name=language]' => array(
            'value' => LANGUAGE_NONE,
          ),
        ),
      ),
    );

    // Make sure the delete buttons shows up last.
    if (isset($form['actions']['delete'])) {
      $form['actions']['delete']['#weight'] = 10;
    }
    $form['#submit'][] = 'i18n_taxonomy_form_term_submit';
  }
}