You are here

function lingotek_form_node_form_alter in Lingotek Translation 7.4

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.5 lingotek.module \lingotek_form_node_form_alter()
  5. 7.6 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 580

Code

function lingotek_form_node_form_alter(&$form, $form_state, $form_id) {

  // On node create, set the language select box to our source language, if this is a node content type that we translate for, if the form is in Add mode.
  if (!isset($form['nid']['#value'])) {

    // add mode (no node id set)
    if (lingotek_supported_type($form['type']['#value'])) {

      // lingotek translated content type
      $form['language']['#default_value'] = lingotek_get_source_language();
    }
  }
  if (!user_access('manage projects')) {
    return;
  }
  $is_enterprise = LingotekAccount::instance()
    ->isEnterprise();
  if (isset($form['nid']['#value'])) {
    $nid = $form['nid']['#value'];
    $node = lingotek_node_load_default($nid);
  }
  else {
    $node = $form['#node'];
    lingotek_node_load(array(
      $node,
    ), array());
  }
  global $language;
  $drupal_language_code = $language->language;
  if ($drupal_language_code != $node->language && lingotek_node_pushed($node)) {
    $lingotek_locale = Lingotek::convertDrupal2Lingotek($drupal_language_code);
    LingotekSync::setTargetStatus($node->nid, $lingotek_locale, LingotekSync::STATUS_PENDING);

    //set to pending so any changes will be downloaded on next sync
    $language_text = lingotek_language_field_lookup('native', $lingotek_locale) . " (" . $drupal_language_code . ")";
    $translation_edit_link = lingotek_get_workbench_url($node->lingotek['document_id'], $lingotek_locale, t('Edit Translation: @language_text', array(
      '@language_text' => $language_text,
    )));
    $edit_translation_message = t('Editing the fields below will only change the content of the source language, not the translation.');
    $edit_translation_message .= "<br/>" . $translation_edit_link;
    drupal_set_message($edit_translation_message, 'warning', FALSE);
  }
  if (!isset($nid)) {
    $nid = NULL;
  }
  $form = array_merge($form, lingotek_get_node_settings_form($form, $form_state, $node));
  $api = LingotekApi::instance();

  // show workflow-change option only if there are more than one workflow and if it's an existing node
  $workflows = $api
    ->listWorkflows();
  if ($workflows && count($workflows) > 1) {
    $form['lingotek']['workflow_id'] = array(
      '#type' => 'select',
      '#title' => t('Workflow'),
      '#prefix' => '<div id="prefill-phases-div">',
      '#description' => t('Choose the Workflow to associate with this content item.'),
      '#default_value' => $node->lingotek['workflow_id'],
      '#options' => $workflows,
      '#empty_option' => '(select one)',
    );
    if (!$nid) {

      // don't show prefill stuff on new nodes
      $form['lingotek']['workflow_id']['#suffix'] = '</div>';
    }
    else {

      // add the callback and checkbox and phase-select
      $form['lingotek']['workflow_id']['#ajax'] = array(
        'callback' => 'lingotek_get_change_workflow_form_callback',
        'wrapper' => 'prefill-phases-div',
        'method' => 'replace',
        'effect' => 'fade',
      );
      $form['lingotek']['prefill_phases_checkbox'] = array(
        '#type' => 'checkbox',
        '#title' => t('Restore to a phase in the new workflow'),
        '#default_value' => TRUE,
        '#description' => t('Prefill the new workflow with translations from the previous workflow'),
        '#states' => array(
          'invisible' => array(
            ':input[name="lingotek[workflow_id]"]' => array(
              'value' => $node->lingotek['workflow_id'],
            ),
          ),
        ),
      );
      $form['lingotek']['prefill_phase_select'] = array(
        '#title' => t("Desired Prefill Phase"),
        '#description' => t('Please select the highest phase which should be prefilled for the new workflow'),
        '#type' => 'select',
        '#states' => array(
          'visible' => array(
            ':input[name="lingotek[workflow_id]"]' => array(
              '!value' => $node->lingotek['workflow_id'],
            ),
            array(
              ':input[name="lingotek[prefill_phases_checkbox]"]' => array(
                'checked' => TRUE,
              ),
            ),
          ),
        ),
        '#suffix' => '</div>',
      );
      if (isset($form_state['values']['lingotek']['workflow_id']) && $form_state['values']['lingotek']['workflow_id'] != NULL) {
        $form['lingotek']['prefill_phase_select']['#options'] = lingotek_get_phases_by_workflow_id($form_state['values']['lingotek']['workflow_id']);
      }
      else {
        $form['lingotek']['prefill_phase_select']['#options'] = array(
          '-1' => '(first choose a workflow)',
        );
        $form['lingotek']['prefill_phase_select']['#disabled'] = TRUE;
      }
    }
  }
  if ($is_enterprise) {

    // Only show these options if the Lingotek document hasn't yet been created.
    if (!$node->lingotek['document_id'] && class_exists('LingotekApi')) {

      // Available projects.
      if ($projects = $api
        ->listProjects()) {
        $form['lingotek']['project_id'] = array(
          '#type' => 'select',
          '#title' => 'Project',
          '#description' => t('Select the translation project with which this item should be associated.'),
          '#default_value' => $node->lingotek['project_id'],
          '#options' => $projects,
        );
      }

      // Translation Memory (TM) Vault.
      if ($vaults = $api
        ->listVaults()) {
        $form['lingotek']['vault_id'] = array(
          '#type' => 'select',
          '#title' => t('TM Vault'),
          '#description' => t('Choose the TM vault to associate with this content item.'),
          '#default_value' => $node->lingotek['vault_id'],
          '#options' => $vaults,
        );
      }
    }

    // END:  Document not created yet
  }
  $form['lingotek']['content_end'] = array(
    '#markup' => '</div>',
  );
  $form['lingotek']['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'developer_settings',
    '#access' => user_access('use lingotek developer tools'),
  );
  $form['lingotek']['advanced']['document_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Document Id'),
    '#description' => t("Read/Overwrite the document ID associated with the document.  This can break the translation process but can also be used to help figure out if something is wrong."),
    '#default_value' => $node->lingotek['document_id'],
  );
  $form['lingotek']['advanced']['current_lingonode'] = array(
    '#type' => 'textarea',
    '#title' => t('Node Data'),
    '#value' => isset($nid) ? json_encode(lingotek_lingonode($nid)) : t('None'),
    '#disabled' => TRUE,
    '#attributes' => array(
      'rows' => '2',
    ),
  );
}