1 translation.module translation_form_node_form_alter(&$form, &$form_state)

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

core/modules/translation/translation.module, line 174
Manages content translations.

Code

function translation_form_node_form_alter(&$form, &$form_state) {
  if (translation_supported_type($form['#node']->type)) {
    $node = $form['#node'];

    // Build two lists with the disabled and enabled languages.
    $languages = language_list();
    $grouped_languages = array();
    foreach ($languages as $langcode => $language) {
      $grouped_languages[(int) $language->enabled][$langcode] = $language;
    }

    $translator_widget = !empty($grouped_languages[0]) && 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('- None -')));
      foreach (array(1, 0) as $status) {
        $group = $groups[$status];
        foreach ($grouped_languages[$status] as $langcode => $language) {
          $options[$group][$langcode] = $language->name;
        }
      }
      $form['langcode']['#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['langcode']['#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['langcode']['#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['langcode']['#options'][$group][$langcode]);
          }
          else {
            unset($form['langcode']['#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.'),
        );
      }
    }
  }
}