diff --git a/config/schema/lingotek.schema.yml b/config/schema/lingotek.schema.yml index 287f014..4c0c501 100644 --- a/config/schema/lingotek.schema.yml +++ b/config/schema/lingotek.schema.yml @@ -32,6 +32,8 @@ lingotek.settings: type: boolean language_specific_profiles: type: boolean + target_download_status: + type: integer account: type: mapping diff --git a/src/Form/LingotekSettingsTabPreferencesForm.php b/src/Form/LingotekSettingsTabPreferencesForm.php index 70c3a60..4ea2167 100644 --- a/src/Form/LingotekSettingsTabPreferencesForm.php +++ b/src/Form/LingotekSettingsTabPreferencesForm.php @@ -126,6 +126,37 @@ class LingotekSettingsTabPreferencesForm extends LingotekConfigFormBase { '#default_value' => $this->lingotek->get('preference.advanced_parsing'), ); + $default_option = array( + 2 => t('Same as source node') + ); + $states = array( + TRUE => t('Published'), + FALSE => t('Unpublished') + ); + // @todo find out how to get the moderation states from both the content_moderation + // and workbench_moderation modules in a form that is akin to the following: + // array( + // machine_name_1 => Name_1, + // machine_name_2 => Name_2, + // ... , + // machine_name_n => Name_n + // ) + // if (\Drupal::moduleHandler()->moduleExists('content_moderation')){ + // $states = array(); @todo see above comment + // } + // elseif (\Drupal::moduleHandler()->moduleExists('workbench_moderation')){ + // $states = array(); @todo see above comment + // } + + $options = array_merge($default_option, $states); + + $form['prefs']['target_download_status'] = array( + '#type' => 'select', + '#description' => t('Translations Download Status: the default status is the status of the source node.'), + '#options' => $options, + '#default_value' => $this->lingotek->get('preference.target_download_status', 2), + ); + $form['prefs']['actions']['#type'] = 'actions'; $form['prefs']['actions']['submit'] = array( '#type' => 'submit', @@ -152,6 +183,7 @@ class LingotekSettingsTabPreferencesForm extends LingotekConfigFormBase { $this->lingotek->set('preference.language_specific_profiles', $form_values['language_specific_profiles']); $this->lingotek->set('preference.advanced_taxonomy_terms', $form_values['advanced_taxonomy_terms']); $this->lingotek->set('preference.advanced_parsing', $form_values['advanced_parsing']); + $this->lingotek->set('preference.target_download_status', $form_values['target_download_status']); $lingotek_config->setDeleteRemoteAfterDisassociation($form_values['delete_tms_documents_upon_disassociation']); parent::submitForm($form, $form_state); } diff --git a/src/LingotekContentTranslationService.php b/src/LingotekContentTranslationService.php index e7bcdc7..ec89a13 100644 --- a/src/LingotekContentTranslationService.php +++ b/src/LingotekContentTranslationService.php @@ -918,6 +918,13 @@ class LingotekContentTranslationService implements LingotekContentTranslationSer // Allow other modules to alter the translation before is saved. \Drupal::moduleHandler()->invokeAll('lingotek_content_entity_translation_presave', [&$translation, $langcode, $data]); + if ($this->lingotekConfiguration->isFieldLingotekEnabled($entity->getEntityTypeId(), $entity->bundle(), 'status')){ + $status = $this->lingotek->get('preference.target_download_status'); + if ($status != "2" ){ + $translation->set('status', $status); + } + } + $translation->save(); return $entity; diff --git a/src/Tests/LingotekNodeTranslationTest.php b/src/Tests/LingotekNodeTranslationTest.php index 7e12af3..813c2f3 100644 --- a/src/Tests/LingotekNodeTranslationTest.php +++ b/src/Tests/LingotekNodeTranslationTest.php @@ -143,6 +143,89 @@ class LingotekNodeTranslationTest extends LingotekTestBase { } /** + * Tests that a node can be translated and set to either published or unpublished + * based on the lingotek setting. + */ + public function testNodeTargetDownloadStatusTranslation() { + // Login as admin. + $this->drupalLogin($this->rootUser); + + $test_image = current($this->drupalGetTestFiles('image')); + + $edit['[prefs][target_download_status]'] = 0; + $this->drupalPostForm('admin/lingotek/settings', $edit, + 'Save', [], [], 'lingoteksettings-tab-preferences-form'); + + // Create a node. + $edit = array(); + $edit['title[0][value]'] = 'Llamas are cool'; + $edit['body[0][value]'] = 'Llamas are very cool'; + $edit['langcode[0][value]'] = 'en'; + $edit['files[field_image_0]'] = drupal_realpath($test_image->uri); + + $this->drupalPostForm('node/add/article', $edit, t('Preview')); + + unset($edit['files[field_image_0]']); + $edit['field_image[0][alt]'] = 'Llamas are cool'; + $this->drupalPostForm(NULL, $edit, t('Save and publish')); + + $this->node = Node::load(1); + + // Check that only the configured fields have been uploaded. + $data = json_decode(\Drupal::state()->get('lingotek.uploaded_content', '[]'), true); + $this->assertUploadedDataFieldCount($data, 3); + $this->assertTrue(isset($data['title'][0]['value'])); + $this->assertEqual(1, count($data['body'][0])); + $this->assertTrue(isset($data['body'][0]['value'])); + $this->assertEqual(1, count($data['field_image'][0])); + $this->assertTrue(isset($data['field_image'][0]['alt'])); + $this->assertIdentical('en_US', \Drupal::state()->get('lingotek.uploaded_locale')); + + // Check that the profile used was the right one. + $used_profile = \Drupal::state()->get('lingotek.used_profile'); + $this->assertIdentical('automatic', $used_profile, 'The automatic profile was used.'); + + // Check that the translate tab is in the node. + $this->drupalGet('node/1'); + $this->clickLink('Translate'); + + // The document should have been automatically uploaded, so let's check + // the upload status. + $this->clickLink('Check Upload Status'); + $this->assertText('The import for node Llamas are cool is complete.'); + + // Request translation. + $this->clickLink('Request translation'); + $this->assertText("Locale 'es_MX' was added as a translation target for node Llamas are cool."); + $this->assertIdentical('es_MX', \Drupal::state()->get('lingotek.added_target_locale')); + + // Check translation status. + $this->clickLink('Check translation status'); + $this->assertIdentical('es_MX', \Drupal::state()->get('lingotek.checked_target_locale')); + $this->assertText('The es_MX translation for node Llamas are cool is ready for download.'); + + // Check that the Edit link points to the workbench and it is opened in a new tab. + $this->assertLinkByHref('/admin/lingotek/workbench/dummy-document-hash-id/es'); + $url = Url::fromRoute('lingotek.workbench', array('doc_id' => 'dummy-document-hash-id', 'locale' => 'es_MX'), array('language' => ConfigurableLanguage::load('es')))->toString(); + $this->assertRaw(''); + // Download translation. + $this->clickLink('Download completed translation'); + $this->assertText('The translation of node Llamas are cool into es_MX has been downloaded.'); + $this->assertIdentical('es_MX', \Drupal::state()->get('lingotek.downloaded_locale')); + + // The content is translated and UNPUBLISHED.I think that we can just do the + // following, but I need to be able to set the preference setting first. + // This translation has been downloaded as unpublished. + $this->assertText('Not published'); + + // The content is translated and unpublished. + $this->clickLink('Las llamas son chulas'); + $this->assertText('Las llamas son chulas'); + $this->assertText('Las llamas son muy chulas'); + $this->assertRaw('node--unpublished'); + } + + /** * Test that when a node is uploaded in a different locale that locale is used. */ public function testAddingContentInDifferentLocale() { @@ -184,4 +267,4 @@ class LingotekNodeTranslationTest extends LingotekTestBase { $this->assertField('lingotek_translation_profile'); } -} \ No newline at end of file +}