You are here

function LingotekContentTranslationForm::buildForm in Lingotek Translation 8

Same name and namespace in other branches
  1. 8.2 src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  2. 4.0.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  3. 3.0.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  4. 3.1.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  5. 3.2.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  6. 3.3.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  7. 3.4.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  8. 3.5.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  9. 3.6.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  10. 3.7.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  11. 3.8.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/LingotekContentTranslationForm.php, line 77
Contains \Drupal\lingotek\Form\LingotekContentTranslationForm.

Class

LingotekContentTranslationForm

Namespace

Drupal\lingotek\Form

Code

function buildForm(array $form, FormStateInterface $form_state, array $build = NULL) {

  /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
  $translation_service = \Drupal::service('lingotek.content_translation');
  $entity = $build['#entity'];
  $entity_type = $entity
    ->getEntityTypeId();
  $document_id = $translation_service
    ->getDocumentId($entity);
  $source_language = $translation_service
    ->getSourceLocale($entity);
  $source_status = $translation_service
    ->getSourceStatus($entity);
  $status_check_needed = $source_status == Lingotek::STATUS_EDITED ? TRUE : FALSE;
  $targets_ready = FALSE;
  $form_state
    ->set('entity', $entity);
  $overview = $build['content_translation_overview'];
  $form['#title'] = $this
    ->t('Translations of @title', array(
    '@title' => $entity
      ->label(),
  ));
  $form['languages'] = array(
    '#type' => 'tableselect',
    '#header' => $overview['#header'],
    '#options' => array(),
  );
  $languages = \Drupal::languageManager()
    ->getLanguages();
  foreach ($languages as $langcode => $language) {
    $locale = $this->languageLocaleMapper
      ->getLocaleForLangcode($langcode);
    $option = array_shift($overview['#rows']);
    $configLanguage = ConfigurableLanguage::load($langcode);
    $enabled = $this->lingotekConfiguration
      ->isLanguageEnabled($configLanguage);

    // Buttons for the ENTITY SOURCE LANGUAGE
    // We disable the checkbox for this row.
    $form['languages'][$langcode] = array(
      '#type' => 'checkbox',
      '#disabled' => $source_language == $locale || !$enabled,
    );
    if ($source_language == $locale) {

      // Check-Progress button if the source upload status is PENDING.
      if ($enabled && ($source_status === Lingotek::STATUS_IMPORTING || $source_status === Lingotek::STATUS_EDITED) && !empty($document_id)) {
        $checkPath = '/admin/lingotek/entity/check_upload/' . $document_id;
        $path = '/admin/lingotek/batch/uploadSingle/' . $entity_type . '/' . $entity
          ->id();
        $this
          ->addOperationLink($entity, $option, 'Check Upload Status', $checkPath, $language);
        $this
          ->addOperationLink($entity, $option, 'Upload', $path, $language);
      }
      elseif ($enabled && ($source_status === Lingotek::STATUS_EDITED || $source_status === Lingotek::STATUS_ERROR || $source_status === NULL)) {
        $path = '/admin/lingotek/batch/uploadSingle/' . $entity_type . '/' . $entity
          ->id();
        $this
          ->addOperationLink($entity, $option, 'Upload', $path, $language);
      }
      elseif ($enabled && $source_status === Lingotek::STATUS_CURRENT) {

        // Allow to re-upload if the status is current.
        $path = '/admin/lingotek/batch/uploadSingle/' . $entity_type . '/' . $entity
          ->id();
        $this
          ->addOperationLink($entity, $option, 'Upload', $path, $language);
      }
    }
    if ($source_language !== $locale && $enabled) {

      // Buttons for the ENTITY TARGET LANGUAGE
      $target_status = $translation_service
        ->getTargetStatus($entity, $langcode);

      // Add-Targets button if languages haven't been added, or if target status is UNTRACKED.
      if (($source_status === Lingotek::STATUS_CURRENT || $source_status === Lingotek::STATUS_IMPORTING) && !empty($document_id) && (!isset($target_status) || $target_status === Lingotek::STATUS_UNTRACKED || $target_status === Lingotek::STATUS_EDITED || $target_status === Lingotek::STATUS_REQUEST)) {
        $path = '/admin/lingotek/entity/add_target/' . $document_id . '/' . $locale;
        $this
          ->addOperationLink($entity, $option, 'Request translation', $path, $language);
      }
      elseif ($target_status === Lingotek::STATUS_PENDING && $source_status === Lingotek::STATUS_CURRENT) {
        $this
          ->removeOperationLink($entity, $option, 'Add');

        //maintain core functionality
        $path = '/admin/lingotek/entity/check_target/' . $document_id . '/' . $locale;
        $this
          ->addOperationLink($entity, $option, 'Check translation status', $path, $language);
        $status_check_needed = TRUE;
      }
      elseif (($target_status === Lingotek::STATUS_READY || $target_status === Lingotek::STATUS_CURRENT) && $source_status === Lingotek::STATUS_CURRENT) {
        $this
          ->removeOperationLink($entity, $option, 'Add');

        //maintain core functionality
        $path = '/admin/lingotek/entity/download/' . $document_id . '/' . $locale;
        $this
          ->addOperationLink($entity, $option, 'Download completed translation', $path, $language);
        $path = '/admin/lingotek/workbench/' . $document_id . '/' . $locale;
        $this
          ->addOperationLink($entity, $option, 'Edit', $path, $language);
        $targets_ready = TRUE;
      }
    }
    $form['languages']['#options'][$langcode] = $option;
  }
  $form['actions']['#type'] = 'actions';
  if ($status_check_needed) {
    $form['actions']['request'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Check Progress'),
      '#submit' => array(
        '::submitForm',
      ),
      '#button_type' => 'primary',
    );
  }
  elseif ($targets_ready) {
    $form['actions']['request'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Download selected translations'),
      '#submit' => array(
        '::submitForm',
      ),
      '#button_type' => 'primary',
    );
  }
  $form['fieldset']['entity'] = array(
    '#type' => 'value',
    '#value' => $entity,
  );
  return $form;
}