You are here

function tmgmt_translation_review_form_revert in Translation Management Tool 8

Review form revert action callback.

1 string reference to 'tmgmt_translation_review_form_revert'
JobItemForm::buildActions in src/Form/JobItemForm.php
Builds the actions for a data item.

File

./tmgmt.module, line 746
Main module file for the Translation Management module.

Code

function tmgmt_translation_review_form_revert(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\tmgmt\JobItemInterface $item */
  $item = $form_state
    ->getFormObject()
    ->getEntity();
  $key = \Drupal::service('tmgmt.data')
    ->ensureArrayKey($form_state
    ->getTriggeringElement()['#data_item_key']);
  if ($item
    ->dataItemRevert($key)) {

    // Update the form_state input values so that the new default vale will be
    // shown.
    $form_key = str_replace('][', '|', $form_state
      ->getTriggeringElement()['#data_item_key']);
    $user_input = $form_state
      ->getUserInput();
    unset($user_input[$form_key]['translation']);

    // This will set the reverted user input to the current text area.
    $user_input[$form_key]['translation'] = $item
      ->getData([
      $key[0],
      '0',
      'value',
      '#translation',
      '#text',
    ]);
    $form_state
      ->setUserInput($user_input);
    $item
      ->save();
  }
  else {
    \Drupal::messenger()
      ->addWarning(t('No past revision found, translation was not reverted.'));
  }
  $form_state
    ->setRebuild();
}