You are here

public function SendContextActionApproveForm::submitForm in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8 src/Form/SendContextActionApproveForm.php \Drupal\tmgmt_smartling\Form\SendContextActionApproveForm::submitForm()
  2. 8.2 src/Form/SendContextActionApproveForm.php \Drupal\tmgmt_smartling\Form\SendContextActionApproveForm::submitForm()
  3. 8.3 src/Form/SendContextActionApproveForm.php \Drupal\tmgmt_smartling\Form\SendContextActionApproveForm::submitForm()

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/SendContextActionApproveForm.php, line 217

Class

SendContextActionApproveForm
Provides a confirmation form for sending multiple content entities.

Namespace

Drupal\tmgmt_smartling\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $current_user_id = $this
    ->currentUser()
    ->id();
  $temp_storage_name = $this
    ->getTempStorageName();

  // Clear out the accounts from the temp store.
  $this->tempStoreFactory
    ->get($temp_storage_name)
    ->delete($current_user_id);
  if (!$form_state
    ->getValue('confirm')) {
    return;
  }

  //@todo:change this when we add the support for queues.
  $is_batch = TRUE;
  $operations = [];
  foreach ($this->entityIds as $id => $entity_type) {

    // Make sure all submissions exists.
    $item_data = [
      'entity_type' => $entity_type,
      'entity_id' => $id,
    ];
    if ($is_batch) {
      $operations[] = [
        [
          get_class($this),
          'processBatch',
        ],
        [
          $item_data,
        ],
      ];
    }
    else {
      $this->queue
        ->createItem($item_data);
    }
  }
  try {
    $ids = array_keys($this->entityIds);
    $job_item_id = reset($ids);
    $translator = JobItem::load($job_item_id)
      ->getTranslator();
    if (!empty($translator)) {
      $translator_settings = $translator
        ->getSettings();

      // Save user name before user switching. We have to use shared storage because we will switch
      // users but we will need to switch user back in the end of the batch.
      $this->sharedStoreFactory
        ->get($temp_storage_name)
        ->set('user_name_before_switching', $this
        ->currentUser()
        ->getAccountName());
      $this->contextUserAuth
        ->switchUser($translator_settings['contextUsername'], $translator_settings['context_silent_user_switching']);
    }
    else {
      $this
        ->messenger()
        ->addError(t('Context was not sent to Smartling, because Smartling provider was not selected for one of these jobs: @jids', [
        '@jids' => implode(' ,', $ids),
      ]));
    }
  } catch (Exception $e) {
    watchdog_exception('tmgmt_smartling', $e);
  }
  if ($is_batch && $operations) {
    $batch = [
      'title' => t('Uploading to Smartling'),
      'operations' => $operations,
      'finished' => [
        get_class($this),
        'finishBatch',
      ],
    ];
    batch_set($batch);
  }
  else {
    $form_state
      ->setRedirect('system.admin_content');
  }
}