public function JobCheckoutManager::doBatchSubmit

Batch callback for submitting a job.

Parameters

int $job_id: The job ID to submit.

int|null $template_job_id: (optional) A template job to use for the translator and settings.

File

src/JobCheckoutManager.php, line 247

Class

JobCheckoutManager
Provides functionality related to job checkout and submissions.

Namespace

Drupal\tmgmt

Code

public function doBatchSubmit($job_id, $template_job_id = NULL) {

  /** @var \Drupal\tmgmt\JobInterface $job */
  $job = $this->entityTypeManager
    ->getStorage('tmgmt_job')
    ->load($job_id);
  if (!$job) {
    return;
  }

  // Delete duplicates.
  if ($existing_items_ids = $job
    ->getConflictingItemIds()) {
    $item_storage = $this->entityTypeManager
      ->getStorage('tmgmt_job_item');
    if (count($existing_items_ids) == $job
      ->getItems()) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('All job items for job @label are conflicting, the job can not be submitted.', [
        '@label' => $job
          ->label(),
      ]));
      return;
    }
    $item_storage
      ->delete($item_storage
      ->loadMultiple($existing_items_ids));
    $num_of_items = count($existing_items_ids);
    $this
      ->messenger()
      ->addWarning($this
      ->getStringTranslation()
      ->formatPlural($num_of_items, '1 conflicting item has been dropped for job @label.', '@count conflicting items have been dropped for job @label.', [
      '@label' => $job
        ->label(),
    ]));
  }
  if ($template_job_id && $job_id != $template_job_id) {

    /** @var \Drupal\tmgmt\JobInterface $template_job */
    $template_job = $this->entityTypeManager
      ->getStorage('tmgmt_job')
      ->load($template_job_id);
    if ($template_job) {
      $job
        ->set('translator', $template_job
        ->getTranslatorId());
      $job
        ->set('settings', $template_job
        ->get('settings')
        ->getValue());

      // If there is a custom label on the template job, copy that as well.
      if ($template_job
        ->get('label')->value) {
        $job
          ->set('label', $template_job
          ->get('label')->value);
      }
    }
  }
  $translator = $job
    ->getTranslator();

  // Check translator availability.
  $translatable_status = $translator
    ->checkTranslatable($job);
  if (!$translatable_status
    ->getSuccess()) {
    $this
      ->messenger()
      ->addError($this
      ->t('Job @label is not translatable with the chosen settings: @reason', [
      '@label' => $job
        ->label(),
      '@reason' => $translatable_status
        ->getReason(),
    ]));
    return;
  }
  if ($this
    ->requestTranslation($job)) {
    $this->jobQueue
      ->markJobAsProcessed($job);
  }
}