You are here

public function RequestTranslationApproveForm::buildForm in TMGMT Extension Suite 8

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 FormInterface::buildForm

File

src/Form/RequestTranslationApproveForm.php, line 117

Class

RequestTranslationApproveForm

Namespace

Drupal\tmgmt_extension_suit\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // TODO: if form uses ajax for refreshing itself on 3rd ajax request
  // it loses $this->tempStore and $this->translatorManager properties.
  // Investigate and fix this.
  $this
    ->refreshServices();
  $data = $this->tempStore
    ->get('request_translation_batch');

  // Empty private storage - nothing to translate.
  if (empty($data)) {
    return $form;
  }
  $form['translator_wrapper'] = [
    '#type' => 'details',
    '#title' => t('Configure provider'),
    '#weight' => 20,
    '#prefix' => '<div id="tmgmt-ui-translator-wrapper">',
    '#suffix' => '</div>',
    '#open' => TRUE,
  ];
  if (!($translators = tmgmt_translator_labels_flagged())) {
    drupal_set_message(t('There are no providers available. Before you can checkout you need to @configure at least one provider.', [
      '@configure' => Link::fromTextAndUrl(t('configure'), Url::fromRoute('entity.tmgmt_translator.collection'))
        ->toString(),
    ]), 'warning');
  }

  // Fake job object. It's only needed for generating checkout settings form.
  // Function tmgmt_job_create just instantiates an object of a given type.
  // We don't want ot save this object to database.
  $job = tmgmt_job_create('en', 'fr', Drupal::currentUser()
    ->id());
  $preselected_translator = $form_state
    ->getValue('translator') ?: key($translators);
  $job->translator = $preselected_translator;
  $form_state
    ->set('job', $job);
  $form['translator_wrapper']['translator'] = [
    '#type' => 'select',
    '#title' => t('Provider'),
    '#description' => t('The configured provider that will process the translation.'),
    '#options' => $translators,
    '#access' => !empty($translators),
    '#default_value' => $preselected_translator,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxTranslatorSelect',
      ],
      'wrapper' => 'tmgmt-ui-translator-wrapper',
    ],
  ];
  try {
    $settings = $this
      ->checkoutSettingsForm($form_state, $job);
  } catch (TMGMTException $e) {
    watchdog_exception('tmgmt_extension_suit', $e);
    $settings = [];
  }
  if (!is_array($settings)) {
    $settings = [];
  }
  $form['translator_wrapper']['settings'] = [
    '#type' => 'details',
    '#title' => t('Checkout settings'),
    '#prefix' => '<div id="tmgmt-ui-translator-settings">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#open' => TRUE,
  ] + $settings;
  $available_languages = tmgmt_available_languages();
  unset($available_languages[$data['source_language']]);
  $form['target_language'] = [
    '#title' => t('Target language'),
    '#type' => 'checkboxes',
    '#options' => $available_languages,
    '#default_value' => array_filter($data['default_target_languages']),
    '#prefix' => '<div id="edit-clone-lang-options">',
    '#suffix' => '<div class="check-controls"><span class="check-all">' . t('Check All') . '</span><span class="uncheck-all">' . t('Uncheck All') . '</span></div></div>',
    '#required' => TRUE,
  ];
  $form['request_translation'] = [
    '#weight' => 20,
    '#type' => 'submit',
    '#button_type' => 'primary',
    '#value' => $this
      ->t('Request translation'),
  ];
  $form['#attached']['library'][] = 'tmgmt_extension_suit/check_uncheck_all';
  return $form;
}