Error message

Warning: count(): Parameter must be an array or an object that implements Countable in _api_make_match_member_link() (line 1230 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).

public function JobItemForm::trackChangedSource

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()

File

src/Form/JobItemForm.php, line 705

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 (!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);
}