You are here

function tmgmt_smartling_tmgmt_job_edit_form_submit in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 tmgmt_smartling.module \tmgmt_smartling_tmgmt_job_edit_form_submit()

Submit tmgmt_smartling checkout job form.

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

1 string reference to 'tmgmt_smartling_tmgmt_job_edit_form_submit'
tmgmt_smartling_form_tmgmt_job_edit_form_alter in ./tmgmt_smartling.module
Implements hook_form_FORM_ID_alter().

File

./tmgmt_smartling.module, line 500
Contains

Code

function tmgmt_smartling_tmgmt_job_edit_form_submit(array &$form, FormStateInterface $form_state) {
  $batch_uid = FALSE;
  $settings = $form_state
    ->getValue('settings');
  $job = $form_state
    ->getFormObject()
    ->getEntity();
  $translator = $job
    ->getTranslator();
  switch ($settings['switcher']) {
    case TMGMT_SMARTLING_CREATE_JOB:
      $job_result = tmgmt_smartling_create_job_form_submit($settings, $translator, $form_state);
      break;
    case TMGMT_SMARTLING_ADD_TO_JOB:
      $job_result = tmgmt_smartling_add_to_job_form_submit($settings, $translator, $form_state);
      break;
  }
  $job_queue = Drupal::getContainer()
    ->get('tmgmt.queue');
  $queue_jobs = $job_queue
    ->getAllJobs();
  $batch_execute_on_job = !empty($queue_jobs) ? end($queue_jobs) : $job;
  if (!empty($job_result['job_id'])) {
    $batch_uid = $translator
      ->getPlugin()
      ->getApiWrapper($translator
      ->getSettings())
      ->createBatch($job_result['job_id'], $job_result['authorize']);
  }
  if (empty($batch_uid)) {
    drupal_set_message(t('Files have not been uploaded. See <a href="@url">logs</a> for more information.', [
      '@url' => Url::fromUri('internal:/admin/reports/dblog')
        ->toString(),
    ]), 'error');
    return;
  }
  $job_settings = $form_state
    ->getValue('settings') + [
    'batch_uid' => $batch_uid,
    'batch_execute_on_job' => $batch_execute_on_job
      ->id(),
  ] + $job_result;
  $form_state
    ->setValue('settings', $job_settings);
  $queue_jobs = empty($queue_jobs) ? [
    $job,
  ] : $queue_jobs;
  $async_mode = $job
    ->getTranslator()
    ->getSetting('async_mode');
  $jobs_in_batch = [];
  foreach ($queue_jobs as $log_job) {
    $jobs_in_batch[] = $log_job
      ->id();
  }
  Drupal::getContainer()
    ->get('logger.channel.smartling')
    ->info(t('Batch info (request translation): uid = "@batch_uid", jobs count = "@jobs_count", jobs = "@jobs_in_batch", execute on job = "@batch_execute_on_job", async mode = "@async_mode"', [
    '@batch_uid' => $job_settings['batch_uid'],
    '@jobs_count' => count($jobs_in_batch),
    '@jobs_in_batch' => implode(', ', $jobs_in_batch),
    '@batch_execute_on_job' => $job_settings['batch_execute_on_job'],
    '@async_mode' => $async_mode,
  ])
    ->render());
  if ($async_mode) {

    // Save original form object.
    $job->settings = $job_settings;
    $job->translator = $translator
      ->id();
    $job
      ->submitted('Job has been put into upload queue.');

    // Save left jobs and add them into upload queue.
    foreach ($queue_jobs as $queue_job) {
      if ($job
        ->id() != $queue_job
        ->id()) {
        $queue_job->settings = $job_settings;
        $queue_job->translator = $translator
          ->id();
        $queue_job
          ->submitted('Job has been put into upload queue.');
      }
      Drupal::service('tmgmt_extension_suit.utils.queue_unique_item')
        ->addItem('tmgmt_extension_suit_upload', [
        'id' => (int) $queue_job
          ->id(),
        'batch_uid' => $batch_uid,
        'batch_execute_on_job' => $batch_execute_on_job
          ->id(),
      ], TRUE);
    }
    drupal_set_message(t('Files have been added into upload queue.'));
  }
}