You are here

public function JobItemForm::trackChangedSource in Translation Management Tool 8

Detect source changes and persist on $form_state.

Parameters

array $data: The data items.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

1 call to JobItemForm::trackChangedSource()
JobItemForm::form in src/Form/JobItemForm.php
Overrides Drupal\Core\Entity\EntityForm::form().

File

src/Form/JobItemForm.php, line 689

Class

JobItemForm
Form controller for the job item edit forms.

Namespace

Drupal\tmgmt\Form

Code

public function trackChangedSource(array $data, FormStateInterface $form_state) {
  $item = $this->entity;
  $source_changed = [];
  $source_removed = [];
  foreach ($data as $key => $value) {
    if (is_array($value) && isset($value['#translate']) && $value['#translate']) {
      $key_array = \Drupal::service('tmgmt.data')
        ->ensureArrayKey($key);
      try {
        $new_data = \Drupal::service('tmgmt.data')
          ->flatten($item
          ->getSourceData());
      } catch (TMGMTException $e) {
        $this
          ->messenger()
          ->addError(t('The source does not exist any more.'));
        return;
      }
      $current_data = $item
        ->getData($key_array);
      if (!isset($new_data[$key])) {
        $source_changed[$key] = t('This data item has been removed from the source.');
        $source_removed[$key] = TRUE;
      }
      elseif ($current_data['#text'] != $new_data[$key]['#text']) {
        $source_changed[$key] = t('The source has changed.');
      }
    }
  }
  $form_state
    ->set('source_changed', $source_changed);
  $form_state
    ->set('source_removed', $source_removed);
}