diff --git a/src/Form/JobItemForm.php b/src/Form/JobItemForm.php index 976741f..70a9702 100644 --- a/src/Form/JobItemForm.php +++ b/src/Form/JobItemForm.php @@ -329,31 +329,31 @@ class JobItemForm extends TmgmtFormBase { $translator_ui->reviewFormSubmit($form, $form_state, $item); } // Write changes back to item. + $data_service = \Drupal::service('tmgmt.data'); foreach ($form_state->getValues() as $key => $value) { if (is_array($value) && isset($value['translation'])) { // Update the translation, this will only update the translation in case // it has changed. We have two different cases, the first is for nested // texts. - if (is_array($value['translation'])) { - $data = array( - '#text' => $value['translation']['value'], - '#origin' => 'local', - ); + $original_text = is_array($value['translation']) ? $value['translation']['value'] : $value['translation']; + $text = $original_text; + if ($text == '') { + $text = ''; // empty segment-only tags } - else { - $data = array( - '#text' => $value['translation'], - '#origin' => 'local', - ); - } - if ($data['#text'] == '' && $item->isActive() && $form_state->getTriggeringElement()['#value'] != '✓') { + // Initialise the empty translation with empty segment-only tags. + // Otherwise mask the translation's HTML tags. + $data_item = $item->getData($data_service->ensureArrayKey($key)); + \Drupal::moduleHandler()->alter('tmgmt_data_item_text_input', $text, $data_item, $item); + + $data = [ + '#text' => $text, + '#origin' => 'local', + ]; + if ($original_text == '' && $item->isActive() && $form_state->getTriggeringElement()['#value'] != '✓') { $data = NULL; continue; } - - $data_service = \Drupal::service('tmgmt.data'); - $current_data_status = $item->getData($data_service->ensureArrayKey($key))['#status']; - + $current_data_status = $data_item['#status']; $item->addTranslatedData($data, $key, $current_data_status); } } @@ -962,10 +962,16 @@ class JobItemForm extends TmgmtFormBase { * The form element for the data item. */ protected function buildTranslation($item_element, $data_item, $rows, FormStateInterface $form_state, $is_preliminary) { + // @todo Add context that this is translation. + $text = NULL; + if (isset($data_item['#translation']['#text'])) { + $text = $data_item['#translation']['#text']; + \Drupal::moduleHandler()->alter('tmgmt_data_item_text_output', $text, $data_item, $this->entity); + } if (!empty($data_item['#format']) && $this->config('tmgmt.settings')->get('respect_text_format') && !$form_state->has('accept_item')) { $item_element['translation'] = array( '#type' => 'text_format', - '#default_value' => isset($data_item['#translation']['#text']) ? $data_item['#translation']['#text'] : NULL, + '#default_value' => $text, '#title' => t('Translation'), '#disabled' => $this->entity->isAccepted() || $is_preliminary, '#rows' => $rows, @@ -984,7 +990,7 @@ class JobItemForm extends TmgmtFormBase { else { $item_element['translation'] = array( '#type' => 'textarea', - '#default_value' => isset($data_item['#translation']['#text']) ? $data_item['#translation']['#text'] : NULL, + '#default_value' => $text, '#title' => t('Translation'), '#disabled' => $this->entity->isAccepted() || $is_preliminary, '#rows' => $rows, @@ -1036,10 +1042,12 @@ class JobItemForm extends TmgmtFormBase { * The form element for the data item. */ protected function buildSource($item_element, $data_item, $rows, FormStateInterface $form_state) { + $text = $data_item['#text']; + \Drupal::moduleHandler()->alter('tmgmt_data_item_text_output', $text, $data_item, $this->entity); if (!empty($data_item['#format']) && $this->config('tmgmt.settings')->get('respect_text_format') && !$form_state->has('accept_item')) { $item_element['source'] = array( '#type' => 'text_format', - '#default_value' => $data_item['#text'], + '#default_value' => $text, '#title' => t('Source'), '#disabled' => TRUE, '#rows' => $rows, @@ -1058,7 +1066,7 @@ class JobItemForm extends TmgmtFormBase { else { $item_element['source'] = array( '#type' => 'textarea', - '#default_value' => $data_item['#text'], + '#default_value' => $text, '#title' => t('Source'), '#disabled' => TRUE, '#rows' => $rows, diff --git a/src/Tests/TMGMTUiReviewTest.php b/src/Tests/TMGMTUiReviewTest.php index 114c19c..ac53dc2 100644 --- a/src/Tests/TMGMTUiReviewTest.php +++ b/src/Tests/TMGMTUiReviewTest.php @@ -467,7 +467,7 @@ class TMGMTUiReviewTest extends EntityTestBase { // Create the node. $settings = array( - 'title' => $this->randomMachineName(), + 'title' => 'First node', 'type' => 'test_bundle', 'body_test' => array( $this->randomMachineName(), @@ -487,7 +487,11 @@ class TMGMTUiReviewTest extends EntityTestBase { $job_item->save(); // Access to the review form. - $this->drupalGet('admin/tmgmt/items/1'); + $this->drupalGet('admin/tmgmt/items/'. $job_item->id()); + // Check that 'hook_tmgmt_data_item_text_output_alter' has been called. + $data_item = $job_item->getData(); + $this->assertEqual($data_item['title'][0]['value']['#text'], 'First node'); + $this->assertFieldByName('title|0|value[source]', 'Second node'); // Test that all the items are being displayed. $this->assertRaw('name="title|0|value[source]"'); @@ -530,6 +534,21 @@ class TMGMTUiReviewTest extends EntityTestBase { $this->assertEqual($label[0], 'Alternative text'); $label = $this->xpath('//*[@id="tmgmt-ui-element-image-test-single-wrapper"]/table/tbody/tr[3]/td[1]/div[1]/label'); $this->assertEqual($label[0], 'Title'); + + // Check 'hook_tmgmt_data_item_text_input_alter' has been called on saving. + $this->drupalPostForm(NULL, ['title|0|value[translation]' => 'Second node translation'], 'Save'); + // Clean the storage and get the updated job item data. + \Drupal::entityTypeManager()->getStorage('tmgmt_job_item')->resetCache(); + $job_item = JobItem::load($job_item->id()); + $data = $job_item->getData(); + $this->assertEqual($data_item['title'][0]['value']['#text'], 'First node'); + $this->assertEqual($data['title'][0]['value']['#translation']['#text'], 'First node translation'); + + // Access to the review form. + $this->drupalGet('admin/tmgmt/items/'. $job_item->id()); + // Check that 'hook_tmgmt_data_item_text_output_alter' has been called. + $this->assertFieldByName('title|0|value[source]', 'Second node'); + $this->assertFieldByName('title|0|value[translation]', 'Second node translation'); } /** diff --git a/tmgmt.api.php b/tmgmt.api.php index b33db9e..c730635 100644 --- a/tmgmt.api.php +++ b/tmgmt.api.php @@ -6,6 +6,7 @@ */ use Drupal\tmgmt\JobInterface; +use Drupal\tmgmt\JobItemInterface; /** * @addtogroup tmgmt_source @@ -297,3 +298,31 @@ function hook_tmgmt_job_after_request_translation(JobInterface $job) { } } } + +/** + * Allows to alter a text's segment unmasking the HTML tags from a tmgmt-tag. + * + * @param string $text + * The text's segment to alter. + * @param array $data_item + * The data item. + * @param \Drupal\tmgmt\JobItemInterface $job_item + * The job item context. + */ +function hook_tmgmt_data_item_text_output_alter(&$text, $data_item, JobItemInterface $job_item) { + $text = str_replace('First', 'Second', $text); +} + +/** + * Allows to alter a text's segment masking the HTML tags into a tmgmt-tag. + * + * @param string $text + * The text's segment to alter. + * @param array $data_item + * The data item. + * @param \Drupal\tmgmt\JobItemInterface $job_item + * The job item context. + */ +function hook_tmgmt_data_item_text_input_alter(&$text, $data_item, JobItemInterface $job_item) { + $text = str_replace('Second', 'First', $text); +} diff --git a/tmgmt_test/tmgmt_test.module b/tmgmt_test/tmgmt_test.module index 818ebea..4fcd86f 100644 --- a/tmgmt_test/tmgmt_test.module +++ b/tmgmt_test/tmgmt_test.module @@ -6,6 +6,7 @@ */ use Drupal\tmgmt\JobInterface; +use Drupal\tmgmt\JobItemInterface; /** * Implements hook_tmgmt_translator_info_alter(). @@ -43,3 +44,17 @@ function tmgmt_test_tmgmt_file_text_processor_plugin_info() { ), ); } + +/** + * Implements hook_tmgmt_data_item_text_output_alter(). + */ +function tmgmt_test_tmgmt_data_item_text_output_alter(&$text, $data_item, JobItemInterface $job_item) { + $text = str_replace('First', 'Second', $text); +} + +/** + * Implements hook_tmgmt_data_item_text_input_alter(). + */ +function tmgmt_test_tmgmt_data_item_text_input_alter(&$text, $data_item, JobItemInterface $job_item) { + $text = str_replace('Second', 'First', $text); +}