diff --git a/src/Form/JobForm.php b/src/Form/JobForm.php index fa67db5..4a47f86 100644 --- a/src/Form/JobForm.php +++ b/src/Form/JobForm.php @@ -59,6 +59,9 @@ class JobForm extends TmgmtFormBase { $target = '?'; } else { + if($form_state->get('check_target_language') == FALSE) { + $form_state->set('check_target_language', $job->getTargetLangcode()); + } $target = $job->getTargetLanguage()->getName(); } @@ -174,8 +177,7 @@ class JobForm extends TmgmtFormBase { } // Checkout whether given source already has items in translation. - $num_of_existing_items = count($this->checkoutExistingItems()); - + $num_of_existing_items = count($this->checkoutExistingItems($form_state->get('check_target_language'))); $form['message'] = array( '#type' => 'label', '#title' => \Drupal::translation()->formatPlural($num_of_existing_items, 'Already exist item in translation.', 'Already exist @count items in translation.'), @@ -504,6 +506,12 @@ class JobForm extends TmgmtFormBase { * Overrides Drupal\Core\Entity\EntityForm::save(). */ public function save(array $form, FormStateInterface $form_state) { + + $ids_of_existing_items = $this->checkoutExistingItems($form_state->get('check_target_language')); + if ($ids_of_existing_items) { + entity_delete_multiple('tmgmt_job_item', $ids_of_existing_items); + } + parent::save($form, $form_state); // Everything below this line is only invoked if the 'Submit to provider' @@ -586,7 +594,7 @@ class JobForm extends TmgmtFormBase { * target / source language dropdowns. */ public function ajaxLanguageSelect(array $form, FormStateInterface $form_state) { - $number_of_existing_items = count($this->checkoutExistingItems()); + $number_of_existing_items = count($this->checkoutExistingItems($form_state->get('check_target_language'))); $replace = $form_state->getUserInput()['_triggering_element_name'] == 'source_language' ? 'target_language' : 'source_language'; $response = new AjaxResponse(); $response->addCommand(new ReplaceCommand('#tmgmt-ui-translator-wrapper', $form['translator_wrapper'])); @@ -610,7 +618,7 @@ class JobForm extends TmgmtFormBase { /** * Helper function for retrieving number of existing items. */ - function checkoutExistingItems() { + function checkoutExistingItems($check_target_language) { $job = $this->entity; $items = $job->getItems(); $latest_items_of_chosen_source = array(); @@ -619,7 +627,7 @@ class JobForm extends TmgmtFormBase { $latest_items_of_chosen_source[$item->id()] = tmgmt_job_item_load_latest($item->getPlugin(), $item->getItemType(), $item->getItemId(), $job->getSourceLangcode()); } foreach ($latest_items_of_chosen_source as $key => $item) { - if (array_key_exists($job->getTargetLangcode(), $item)) { + if (($check_target_language == FALSE || $check_target_language !== $job->getTargetLangcode()) && array_key_exists($job->getTargetLangcode(), $item)) { $ids_of_existing_items[] = $key; } }