diff -u b/src/Form/JobItemForm.php b/src/Form/JobItemForm.php --- b/src/Form/JobItemForm.php +++ b/src/Form/JobItemForm.php @@ -335,26 +335,16 @@ // 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', - ); - } - else { - $data = array( - '#text' => $value['translation'], - '#origin' => 'local', - ); - } - - // Unmask the translation's HTML tags. + $text = is_array($value['translation']) ? $value['translation']['value'] : $value['translation']; $data_item = $item->getData($data_service->ensureArrayKey($key)); - if (isset($data_item['#translation']['#text'])) { - $text = $data_item['#translation']['#text']; - \Drupal::moduleHandler()->alter('tmgmt_data_item_text_input', $text, $data_item, $item); - } + // Mask the translation's HTML tags. + \Drupal::moduleHandler()->alter('tmgmt_data_item_text_input', $text, $data_item, $item); + + $data = [ + '#text' => $text, + '#origin' => 'local', + ]; if ($data['#text'] == '' && $item->isActive() && $form_state->getTriggeringElement()['#value'] != '✓') { $data = NULL; continue; diff -u b/tmgmt_test/tmgmt_test.module b/tmgmt_test/tmgmt_test.module --- b/tmgmt_test/tmgmt_test.module +++ b/tmgmt_test/tmgmt_test.module @@ -48,7 +48,7 @@ /** * Implements hook_tmgmt_data_item_text_output_alter(). */ -function tmgmt_test_tmgmt_data_item_text_output_alter(&$text, $data_item, JobItemInterface &$job_item) { +function tmgmt_test_tmgmt_data_item_text_output_alter(&$text, $data_item, JobItemInterface $job_item) { $text = str_replace('First', 'Second', $text); } only in patch2: unchanged: --- 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'); } /** only in patch2: unchanged: --- 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); +}