You are here

function lingotek_form_node_form_alter in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 7.7 lingotek.module \lingotek_form_node_form_alter()
  2. 7.2 lingotek.module \lingotek_form_node_form_alter()
  3. 7.3 lingotek.module \lingotek_form_node_form_alter()
  4. 7.4 lingotek.module \lingotek_form_node_form_alter()
  5. 7.5 lingotek.module \lingotek_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

Parameters

array $form: A FAPI form array for the form being altered.

array $form_state: A FAPI form state array for the form being altered.

string $form_id: The ID of the form being altered.

File

./lingotek.module, line 653

Code

function lingotek_form_node_form_alter(&$form, $form_state, $form_id) {
  if (!user_access('manage projects')) {
    return;
  }

  // Load the node and also the source node if different (node-based translation)
  $node = $form_state['node'];
  $nid = !empty($node->nid) ? $node->nid : 0;
  lingotek_entity_load(array(
    $nid => $node,
  ), 'node');
  if (!empty($node->tnid)) {
    $source_node = lingotek_entity_load_single('node', $node->tnid);
  }
  else {
    $source_node = $node;
  }
  if (!empty($node->nid)) {

    // Load the most current revision of the node
    $node = lingotek_node_load_default($node->nid);
  }
  $source_nid = lingotek_is_node_translation($node);
  if (!$source_nid && !empty($node->nid)) {
    $source_nid = $node->nid;
  }

  // Get the current language
  global $language;
  $drupal_language = $language->language;
  $ln = LingotekEntity::load($node, 'node');
  $node_language = $ln->language;
  if (lingotek_uses_node_translation($source_node) && $node_language != LANGUAGE_NONE) {

    // a node-based edit, so use the node's language'
    $current_language = $node_language;
  }
  else {

    // must either be a new node or field-based so use $language->language
    $current_language = $drupal_language;
  }

  // Include the translation-management tab always
  $form = lingotek_get_node_settings_form($form, $form_state, $node);

  // Set the default language on new node creation.
  if (empty($source_node->nid) && $form['language']['#default_value'] == LANGUAGE_NONE) {
    $form['language']['#default_value'] = lingotek_get_source_language();
  }

  // No more changes to the node form if the node or language aren't enabled for Lingotek translation
  if (!lingotek_enabled_langcode($current_language) || !lingotek_managed_entity('node', $source_node)) {
    return;
  }

  ///////////////////////////////////////////////////////////////////////

  // FROM HERE DOWN, LINGOTEK MANAGES BOTH THE NODE AND THE LANGUAGE

  ///////////////////////////////////////////////////////////////////////
  $form = array_merge($form, lingotek_get_language_override_form($form, $form_state, $node));

  // Disable the language field if the node is synced with Lingotek.
  if (!empty($node->nid)) {
    $document_id = lingotek_keystore('node', $node->nid, 'document_id');
    if (isset($document_id) && $document_id != 0) {
      $form['language']['#disabled'] = TRUE;
    }
  }

  // Disable all Lingotek-managed fields if the node already exists and the
  // current language isn't the source language and the administrator has not
  // selected the Lingotek preference to allow local editing of translations.
  if (!empty($source_node->nid) && $current_language != $source_node->language && !variable_get('lingotek_allow_local_edits', FALSE)) {
    $lingotek_fields = variable_get('lingotek_enabled_fields');
    $enabled_fields = !empty($lingotek_fields['node'][$node->type]) ? $lingotek_fields['node'][$node->type] : NULL;
    if (!$enabled_fields && lingotek_oneoff_translate($node)) {
      $enabled_fields = lingotek_get_translatable_fields_by_content_type('node', $node->type);
    }
    foreach ($enabled_fields as $field) {
      $form[$field]['#disabled'] = TRUE;
    }
    $workbench = 'node/' . $source_nid . '/lingotekworkbench/' . Lingotek::convertDrupal2Lingotek($current_language);

    //$source_link = base_path() . $source_node->language . '/node/' . $source_nid . '/edit';
    $enabled_languages = language_list();
    $source_language_obj = !empty($enabled_languages[$source_node->language]) ? $enabled_languages[$source_node->language] : $language;
    $source_link = l(t('source content'), 'node/' . $source_nid . '/edit', array(
      'language' => $source_language_obj,
    ));
    $message = t('Some fields are not editable because the translations are managed by lingotek.
      To edit this content in the source language go to the !source_link.', array(
      '!source_link' => $source_link,
    ));
    $link = l(t('Lingotek workbench'), $workbench, array(
      'attributes' => array(
        'target' => '_blank',
      ),
    ));
    $message .= '<br>' . t('To make changes to this translation you can go to the !url.', array(
      '!url' => $link,
    ));
    drupal_set_message($message, 'warning', FALSE);
  }
}