You are here

public function LingotekContentTranslationForm::buildForm in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 8 src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  2. 8.2 src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  3. 4.0.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  4. 3.0.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  5. 3.1.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  6. 3.2.x src/Form/LingotekContentTranslationForm.php \Drupal\lingotek\Form\LingotekContentTranslationForm::buildForm()
  7. 3.3.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 80

Class

LingotekContentTranslationForm

Namespace

Drupal\lingotek\Form

Code

public 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', [
    '@title' => $entity
      ->label(),
  ]);
  $form['languages'] = [
    '#type' => 'tableselect',
    '#header' => $overview['#header'],
    '#options' => [],
  ];
  $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) && \Drupal::currentUser()
      ->hasPermission('manage lingotek translations');

    // Buttons for the ENTITY SOURCE LANGUAGE
    // We disable the checkbox for this row.
    $form['languages'][$langcode] = [
      '#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 === Lingotek::STATUS_UNTRACKED || $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) {
        $path = '/admin/lingotek/entity/check_target/' . $document_id . '/' . $locale;
        $this
          ->addOperationLink($entity, $option, 'Check translation status', $path, $language, TRUE);
        $status_check_needed = TRUE;
      }
      elseif ($target_status === Lingotek::STATUS_READY || $target_status === Lingotek::STATUS_CURRENT) {
        $path = '/admin/lingotek/workbench/' . $document_id . '/' . $locale;
        $this
          ->addOperationLink($entity, $option, 'Edit in Lingotek Workbench', $path, $language, TRUE);
        $path = '/admin/lingotek/entity/download/' . $document_id . '/' . $locale;
        if ($target_status === Lingotek::STATUS_READY) {
          $this
            ->addOperationLink($entity, $option, 'Download completed translation', $path, $language, TRUE);
        }
        elseif ($target_status === Lingotek::STATUS_CURRENT) {
          $this
            ->addOperationLink($entity, $option, 'Re-download completed translation', $path, $language, TRUE);
        }
        $targets_ready = TRUE;
      }
    }
    $form['languages']['#options'][$langcode] = $option;
  }
  if (\Drupal::currentUser()
    ->hasPermission('manage lingotek translations')) {
    $form['actions']['#type'] = 'actions';
    if ($status_check_needed) {
      $form['actions']['request'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Check Progress'),
        '#submit' => [
          '::submitForm',
        ],
        '#button_type' => 'primary',
      ];
    }
    elseif ($targets_ready) {
      $form['actions']['request'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Download selected translations'),
        '#submit' => [
          '::submitForm',
        ],
        '#button_type' => 'primary',
      ];
    }
  }
  $form['fieldset']['entity'] = [
    '#type' => 'value',
    '#value' => $entity,
  ];
  return $form;
}