You are here

public static function SendContextActionApproveForm::processBatch in TMGMT Translator Smartling 8

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

Processes the sending batch.

Parameters

array $data: Keyed array of data to send.

array $context: The batch context.

File

src/Form/SendContextActionApproveForm.php, line 268

Class

SendContextActionApproveForm
Provides a confirmation form for sending multiple content entities.

Namespace

Drupal\tmgmt_smartling\Form

Code

public static function processBatch($data, &$context) {
  if (!isset($context['results']['errors'])) {
    $context['results']['errors'] = [];
    $context['results']['count'] = 0;
  }
  $entity_type_id = $data['entity_type'];
  $entity_id = $data['entity_id'];
  try {
    $job_item = \Drupal::entityTypeManager()
      ->getStorage($entity_type_id)
      ->loadMultiple([
      $entity_id,
    ]);
    $job_item = reset($job_item);
    if (empty($job_item
      ->getTranslator()) || !$job_item
      ->getTranslator()
      ->getPlugin() instanceof SmartlingTranslator) {
      return;
    }
    $job = $job_item
      ->getJob();
    $filename = $job
      ->getTranslatorPlugin()
      ->getFileName($job);

    /** @var ContextUploader $context_uploader */
    $context_uploader = \Drupal::getContainer()
      ->get('tmgmt_smartling.utils.context.uploader');
    $url = $context_uploader
      ->jobItemToUrl($job_item);
    if ($job
      ->hasTranslator()) {
      $settings = $job
        ->getTranslator()
        ->getSettings();
    }
    else {
      \Drupal::logger('smartling')
        ->warning("Job with ID=@id has no translator plugin.", [
        '@id' => $job
          ->id(),
      ]);
      return;
    }
    if ($context_uploader
      ->isReadyAcceptContext($filename, $settings)) {
      $result = $context_uploader
        ->upload($url, $filename, $settings);
    }
    else {
      $result = [];
    }
  } catch (SmartlingBaseException $e) {
    \Drupal::logger('smartling')
      ->error($e
      ->getMessage());
  }
  if (empty($result)) {
    $context['results']['errors'][] = t('Context wasn\'t uploaded. Please see logs for more info.');
  }
  elseif (isset($result['updatedStringsCount'])) {
    $context['message'] = new FormattableMarkup('Context for "@name" was successfully sent. @updatedStringsCount strings updated.', [
      '@name' => $job_item
        ->label(),
      '@updatedStringsCount' => $result['updatedStringsCount'],
    ]);
  }
}