Detect source changes and persist on $form_state.
array $data: The data items.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
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 (!empty($current_data['#text']) && $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);
}