You are here

public function SmartlingTranslatorUi::processSelectedQueues in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 src/SmartlingTranslatorUi.php \Drupal\tmgmt_smartling\SmartlingTranslatorUi::processSelectedQueues()
  2. 8.2 src/SmartlingTranslatorUi.php \Drupal\tmgmt_smartling\SmartlingTranslatorUi::processSelectedQueues()

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

File

src/SmartlingTranslatorUi.php, line 329
Contains \Drupal\tmgmt_smartling\SmartlingTranslatorUi.

Class

SmartlingTranslatorUi
Smartling translator UI.

Namespace

Drupal\tmgmt_smartling

Code

public function processSelectedQueues(array &$form, FormStateInterface $form_state) {
  $selected_queues = array_filter($form_state
    ->getValue('queues'));
  if (!empty($selected_queues)) {
    $batch = [
      'operations' => [],
    ];

    // We can't switch user in a middle of the batch process so unset
    // context queue from processing array if there are some additional
    // queues selected.
    if (count($selected_queues) > 1 && in_array('smartling_context_upload', $selected_queues)) {
      unset($selected_queues['smartling_context_upload']);
      drupal_set_message(t('Please, process "Upload context" queue separately.'), 'warning');
    }
    foreach ($selected_queues as $queue_name) {
      $queue = \Drupal::service('queue')
        ->get($queue_name);
      if ($queue
        ->numberOfItems()) {

        // Special case for context uploading queue: we have to switch user
        // before doing this in a batch and switch user back when we finish.
        if ($queue_name == 'smartling_context_upload') {
          $batch['finished'] = [
            get_class($this),
            'finishBatch',
          ];
          $translator_settings = $form_state
            ->getValue('settings');
          \Drupal::getContainer()
            ->get('user.shared_tempstore')
            ->get(self::TEMP_STORAGE_NAME)
            ->set(self::USER_NAME_BEFORE_SWITCHING, \Drupal::currentUser()
            ->getAccountName());
          try {
            \Drupal::getContainer()
              ->get('tmgmt_smartling.utils.context.user_auth')
              ->switchUser($translator_settings['contextUsername'], $translator_settings['context_silent_user_switching']);
          } catch (Exception $e) {
            watchdog_exception('tmgmt_smartling', $e);
          }
        }
        foreach (range(1, $queue
          ->numberOfItems()) as $index) {
          $batch['operations'][] = [
            '\\Drupal\\tmgmt_smartling\\SmartlingTranslatorUi::step',
            [
              $queue_name,
            ],
          ];
        }
      }
    }
    batch_set($batch);
  }
}