You are here

function translation_form_node_form_alter in Drupal 7

Implements hook_form_BASE_FORM_ID_alter() for node_form().

Alters language fields on node edit forms when a translation is about to be created.

See also

node_form()

File

modules/translation/translation.module, line 134
Manages content translations.

Code

function translation_form_node_form_alter(&$form, &$form_state) {
  if (translation_supported_type($form['#node']->type)) {
    $node = $form['#node'];
    $languages = language_list('enabled');
    $disabled_languages = isset($languages[0]) ? $languages[0] : FALSE;
    $translator_widget = $disabled_languages && user_access('translate content');
    $groups = array(
      t('Disabled'),
      t('Enabled'),
    );

    // Allow translators to enter content in disabled languages. Translators
    // might need to distinguish between enabled and disabled languages, hence
    // we divide them in two option groups.
    if ($translator_widget) {
      $options = array(
        $groups[1] => array(
          LANGUAGE_NONE => t('Language neutral'),
        ),
      );
      $language_list = locale_language_list('name', TRUE);
      foreach (array(
        1,
        0,
      ) as $status) {
        $group = $groups[$status];
        foreach ($languages[$status] as $langcode => $language) {
          $options[$group][$langcode] = $language_list[$langcode];
        }
      }
      $form['language']['#options'] = $options;
    }
    if (!empty($node->translation_source)) {

      // We are creating a translation. Add values and lock language field.
      $form['translation_source'] = array(
        '#type' => 'value',
        '#value' => $node->translation_source,
      );
      $form['language']['#disabled'] = TRUE;
    }
    elseif (!empty($node->nid) && !empty($node->tnid)) {

      // Disable languages for existing translations, so it is not possible to switch this
      // node to some language which is already in the translation set. Also remove the
      // language neutral option.
      unset($form['language']['#options'][LANGUAGE_NONE]);
      foreach (translation_node_get_translations($node->tnid) as $langcode => $translation) {
        if ($translation->nid != $node->nid) {
          if ($translator_widget) {
            $group = $groups[(int) (!isset($disabled_languages[$langcode]))];
            unset($form['language']['#options'][$group][$langcode]);
          }
          else {
            unset($form['language']['#options'][$langcode]);
          }
        }
      }

      // Add translation values and workflow options.
      $form['tnid'] = array(
        '#type' => 'value',
        '#value' => $node->tnid,
      );
      $form['translation'] = array(
        '#type' => 'fieldset',
        '#title' => t('Translation settings'),
        '#access' => user_access('translate content'),
        '#collapsible' => TRUE,
        '#collapsed' => !$node->translate,
        '#tree' => TRUE,
        '#weight' => 30,
      );
      if ($node->tnid == $node->nid) {

        // This is the source node of the translation
        $form['translation']['retranslate'] = array(
          '#type' => 'checkbox',
          '#title' => t('Flag translations as outdated'),
          '#default_value' => 0,
          '#description' => t('If you made a significant change, which means translations should be updated, you can flag all translations of this post as outdated. This will not change any other property of those posts, like whether they are published or not.'),
        );
        $form['translation']['status'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
      }
      else {
        $form['translation']['status'] = array(
          '#type' => 'checkbox',
          '#title' => t('This translation needs to be updated'),
          '#default_value' => $node->translate,
          '#description' => t('When this option is checked, this translation needs to be updated because the source post has changed. Uncheck when the translation is up to date again.'),
        );
      }
    }
  }
}