diff --git a/src/Form/JobItemForm.php b/src/Form/JobItemForm.php index 976741f..aeef358 100644 --- a/src/Form/JobItemForm.php +++ b/src/Form/JobItemForm.php @@ -329,6 +329,7 @@ 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 @@ -346,14 +347,19 @@ class JobItemForm extends TmgmtFormBase { '#origin' => 'local', ); } + + // Unmask the translation's HTML tags. + $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); + } + if ($data['#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 +968,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 +996,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 +1048,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 +1072,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/tmgmt_test/tmgmt_test.module b/tmgmt_test/tmgmt_test.module index 818ebea..5c7d7e6 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); +}