You are here

function translation_form_alter in Internationalization 5.2

Same name and namespace in other branches
  1. 5.3 translation/translation.module \translation_form_alter()
  2. 5 translation/translation.module \translation_form_alter()

Implementation of hook_form_alter().

File

translation/translation.module, line 179

Code

function translation_form_alter($form_id, &$form) {

  // Node add/edit form
  if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id && variable_get('i18n_node_' . $form['type']['#value'], 0)) {
    $node = $form['#node'];

    // Allow node to set it's own language list
    $languages = i18n_node_language_list($node);

    // Translation workflow, default
    if ($node->nid && $node->translation) {
      $translations = $node->translation;
    }
    elseif (isset($node->translation_source) && translation_access($node->translation_source)) {

      // We are translating a node: node/add/$type?translation=$nid&language=$language
      $translation_nid = $node->translation_nid;
      $language = $node->language;

      // Get the node to be translated and populate fields
      $trans = $node->translation_source;
      $form['i18n']['translation_nid'] = array(
        '#type' => 'hidden',
        '#value' => $translation_nid,
      );
      $form['i18n']['language']['#default_value'] = $language;

      // Do not allow language change
      $form['i18n']['language']['#disabled'] = TRUE;
      $form['i18n']['language']['#description'] = t('Language cannot be changed while creating a translation.');
      if ($trans->trid) {
        $form['i18n']['trid'] = array(
          '#type' => 'hidden',
          '#value' => $trans->trid,
        );
      }

      // Translations are taken from source node
      $translations = $trans->translation;
      $translations[$trans->language] = $trans;
    }

    // Display translations and restrict languages
    if ($translations) {

      // Unset invalid languages
      foreach (array_keys($translations) as $lang) {
        unset($form['i18n']['language']['#options'][$lang]);
      }

      // Add translation list
      $form['i18n']['#title'] = t('Language and translations');
      $form['i18n']['translations'] = array(
        '#type' => 'markup',
        '#value' => theme('translation_node_list', $translations, FALSE),
      );
    }

    // Translation workflow
    if (variable_get('i18n_translation_workflow', 1) && (user_access('translate nodes') || user_access('administer nodes'))) {
      $form['i18n']['i18n_status'] = array(
        '#type' => 'select',
        '#title' => t('Translation workflow'),
        '#options' => _translation_status(),
        '#description' => t('Use the translation workflow to keep track of content that needs translation.'),
      );
      if ($node->nid) {
        $form['i18n']['i18n_status']['#default_value'] = isset($node->i18n_status) ? $node->i18n_status : TRANSLATION_STATUS_NONE;
      }
      elseif (isset($trans)) {
        $form['i18n']['i18n_status']['#default_value'] = TRANSLATION_STATUS_WORKING;
      }
    }
    else {
      $form['i18n']['i18n_status'] = array(
        '#type' => 'value',
        '#value' => isset($node->i18n_status) ? $node->i18n_status : TRANSLATION_STATUS_NONE,
      );
    }

    // Clone files for original node ?
    if (isset($trans) && is_array($trans->files) && count($trans->files)) {
      $form['i18n']['translation_files'] = array(
        '#type' => 'fieldset',
        '#title' => t('Files from translated content'),
        '#tree' => TRUE,
        '#prefix' => '<div class="attachments">',
        '#suffix' => '</div>',
        '#theme' => 'upload_form_current',
        '#description' => t('You can remove the files for this translation or keep the original files and translate the description.'),
      );
      foreach ($trans->files as $key => $file) {
        $description = file_create_url(strpos($file->fid, 'upload') === false ? $file->filepath : file_create_filename($file->filename, file_create_path()));
        $description = "<small>" . check_plain($description) . "</small>";
        $form['i18n']['translation_files'][$key]['description'] = array(
          '#type' => 'textfield',
          '#default_value' => strlen($file->description) ? $file->description : $file->filename,
          '#maxlength' => 256,
          '#description' => $description,
        );
        $form['i18n']['translation_files'][$key]['size'] = array(
          '#type' => 'markup',
          '#value' => format_size($file->filesize),
        );
        $form['i18n']['translation_files'][$key]['remove'] = array(
          '#type' => 'checkbox',
          '#default_value' => 0,
        );
        $form['i18n']['translation_files'][$key]['list'] = array(
          '#type' => 'checkbox',
          '#default_value' => $file->list,
        );
        $form['i18n']['translation_files'][$key]['fid'] = array(
          '#type' => 'value',
          '#value' => $file->fid,
        );
      }
    }
  }
}