Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 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;
             drupal_set_message(t('Unable to add job item for target language %name. Make sure the source content is not empty.', array('%name' => $target_lang_name)), 'error');
         }
     }
     tmgmt_job_checkout_and_redirect($form_state, $jobs);
 }
Ejemplo n.º 2
0
 /**
  * Custom form submit callback for tmgmt_cart_cart_form().
  */
 function submitRequestTranslation(array $form, FormStateInterface $form_state)
 {
     $target_languages = array_filter($form_state->getValue('target_language'));
     $enforced_source_language = NULL;
     if ($form_state->getValue('enforced_source_language')) {
         $enforced_source_language = $form_state->getValue('source_language');
     }
     $skipped_count = 0;
     $job_items_by_source_language = array();
     // Group the selected items by source language.
     foreach (JobItem::loadMultiple(array_filter($form_state->getValue('items'))) as $job_item) {
         $source_language = $enforced_source_language ? $enforced_source_language : $job_item->getSourceLangCode();
         if (in_array($source_language, $job_item->getExistingLangCodes())) {
             $job_items_by_source_language[$source_language][$job_item->id()] = $job_item;
         } else {
             $skipped_count++;
         }
     }
     $jobs = array();
     $remove_job_item_ids = array();
     // Loop over all target languages, create a job for each source and target
     // language combination add add the relevant job items to it.
     foreach ($target_languages as $target_language) {
         foreach ($job_items_by_source_language as $source_language => $job_items) {
             // Skip in case the source language is the same as the target language.
             if ($source_language == $target_language) {
                 continue;
             }
             $job = tmgmt_job_create($source_language, $target_language, $this->currentUser()->id());
             $job_empty = TRUE;
             /** @var \Drupal\tmgmt\JobItemInterface $job_item */
             foreach ($job_items as $id => $job_item) {
                 try {
                     // As the same item might be added to multiple jobs, we need to
                     // re-create them and delete the old ones, after removing them from
                     // the cart.
                     $job->addItem($job_item->getPlugin(), $job_item->getItemType(), $job_item->getItemId());
                     $remove_job_item_ids[$job_item->id()] = $job_item->id();
                     $job_empty = FALSE;
                 } catch (Exception $e) {
                     // If an item fails for one target language, then it is also going
                     // to fail for others, so remove it from the array.
                     unset($job_items_by_source_language[$source_language][$id]);
                     drupal_set_message($e->getMessage(), 'error');
                 }
             }
             if (!$job_empty) {
                 $jobs[] = $job;
             }
         }
     }
     // Remove job items from the cart.
     if ($remove_job_item_ids) {
         tmgmt_cart_get()->removeJobItems($remove_job_item_ids);
         entity_delete_multiple('tmgmt_job_item', $remove_job_item_ids);
     }
     // Start the checkout process if any jobs were created.
     if ($jobs) {
         if ($enforced_source_language) {
             drupal_set_message(t('You have enforced the job source language which most likely resulted in having a translation of your original content as the job source text. You should review the job translation received from the translator carefully to prevent the content quality loss.'), 'warning');
             if ($skipped_count) {
                 $languages = \Drupal::languageManager()->getLanguages();
                 drupal_set_message(\Drupal::translation()->formatPlural($skipped_count, 'One item skipped as for the language @language it was not possible to retrieve a translation.', '@count items skipped as for the language @language it was not possible to retrieve a translations.', array('@language' => $languages[$enforced_source_language]->getName())));
             }
         }
         tmgmt_job_checkout_and_redirect($form_state, $jobs);
     } else {
         drupal_set_message(t('From the selection you made it was not possible to create any translation job.'));
     }
 }