function translation_form_node_form_alter

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

drupal/core/modules/translation/translation.module, line 198
Manages content translations.

Code

function translation_form_node_form_alter(&$form, &$form_state) {
  $node = $form_state['controller']
    ->getEntity();
  if (translation_supported_type($node->type)) {
    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::LANGCODE_NOT_SPECIFIED]);
      foreach (translation_node_get_translations($node->tnid) as $langcode => $translation) {
        if ($translation->nid != $node->nid) {
          unset($form['langcode']['#options'][$langcode]);
        }
      }

      // Add translation values and workflow options.
      $form['tnid'] = array(
        '#type' => 'value',
        '#value' => $node->tnid,
      );
      $form['translation'] = array(
        '#type' => 'details',
        '#title' => t('Translation settings'),
        '#access' => translation_user_can_translate_node($node),
        '#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.'),
        );
      }
    }
  }
}