function ContentTranslateForm::submitForm

File

sources/content/src/Form/ContentTranslateForm.php, line 127

Class

ContentTranslateForm

Namespace

Drupal\tmgmt_content\Form

Code

function submitForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $form_state
    ->get('entity');
  $values = $form_state
    ->getValues();
  $jobs = array();
  foreach (array_keys(array_filter($values['languages'])) as $langcode) {

    // Create the job object.
    $job = tmgmt_job_create($entity
      ->language()
      ->getId(), $langcode, \Drupal::currentUser()
      ->id());
    try {

      // Add the job item.
      $job
        ->addItem('content', $entity
        ->getEntityTypeId(), $entity
        ->id());

      // Append this job to the array of created jobs so we can redirect the user
      // to a multistep checkout form if necessary.
      $jobs[$job
        ->id()] = $job;
    } catch (TMGMTException $e) {
      watchdog_exception('tmgmt', $e);
      $languages = \Drupal::languageManager()
        ->getLanguages();
      $target_lang_name = $languages[$langcode]->language;
      $this
        ->messenger()
        ->addError(t('Unable to add job item for target language %name. Make sure the source content is not empty.', array(
        '%name' => $target_lang_name,
      )));
    }
  }
  \Drupal::service('tmgmt.job_checkout_manager')
    ->checkoutAndRedirect($form_state, $jobs);
}