protected function JobItemForm::buildChangedSource

Builds the notification and diff for source changes for a data item.

Parameters

array $item_element: The form element for the data item.

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

string $field_name: The name of the form element.

string $key: The data item key for the given structure.

string $ajax_id: The ID used for ajax replacements.

Return value

array The form element for the data item.

1 call to JobItemForm::buildChangedSource()
JobItemForm::reviewFormElement in src/Form/JobItemForm.php
Build form elements for the review form using flattened data items.

File

src/Form/JobItemForm.php, line 881

Class

JobItemForm
Form controller for the job item edit forms.

Namespace

Drupal\tmgmt\Form

Code

protected function buildChangedSource($item_element, FormStateInterface $form_state, $field_name, $key, $ajax_id) {

  // Check for source changes and offer actions.
  if (isset($form_state
    ->get('source_changed')[$key])) {

    // Show diff if requested.
    if ($form_state
      ->get('show_diff:' . $key)) {
      $keys = \Drupal::service('tmgmt.data')
        ->ensureArrayKey($field_name);
      try {
        $new_data = \Drupal::service('tmgmt.data')
          ->flatten($this->entity
          ->getSourceData());
      } catch (TMGMTException $e) {
        $new_data = [];
      }
      $current_data = $this->entity
        ->getData($keys);
      $diff_header = [
        '',
        t('Current text'),
        '',
        t('New text'),
      ];
      $current_lines = explode("\n", $current_data['#text']);
      $new_lines = explode("\n", isset($new_data[$key]) ? $new_data[$key]['#text'] : '');
      $diff_formatter = new DiffFormatter($this
        ->configFactory());
      $diff = new Diff($current_lines, $new_lines);
      $diff_rows = $diff_formatter
        ->format($diff);

      // Unset start block.
      unset($diff_rows[0]);
      $item_element['below']['source_changed']['diff'] = [
        '#type' => 'table',
        '#header' => $diff_header,
        '#rows' => $diff_rows,
        '#empty' => $this
          ->t('No visible changes'),
        '#attributes' => [
          'class' => [
            'diff',
          ],
        ],
      ];
      $item_element['below']['source_changed']['#attached']['library'][] = 'system/diff';
      $item_element['below_actions']['resolve-diff'] = [
        '#type' => 'submit',
        '#value' => t('Resolve'),
        '#attributes' => [
          'title' => t('Apply the changes of the source.'),
        ],
        '#name' => 'resolve-diff-' . $field_name,
        '#data_item_key' => $key,
        '#submit' => [
          '::resolveDiff',
        ],
        '#ajax' => [
          'callback' => '::ajaxReviewForm',
          'wrapper' => $ajax_id,
        ],
      ];
    }
    else {
      $item_element['below']['source_changed'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'tmgmt_source_changed',
            'messages',
            'messages--warning',
          ],
        ],
      ];

      // Display changed message.
      $item_element['below']['source_changed']['message'] = [
        '#markup' => '<span>' . $form_state
          ->get('source_changed')[$key] . '</span>',
        '#attributes' => [
          'class' => [
            'tmgmt-review-message-inline',
          ],
        ],
      ];
      if (!isset($form_state
        ->get('source_removed')[$key])) {

        // Offer diff action.
        $item_element['below']['source_changed']['diff_button'] = [
          '#type' => 'submit',
          '#value' => t('Show change'),
          '#name' => 'diff-button-' . $field_name,
          '#data_item_key' => $key,
          '#submit' => [
            '::showDiff',
          ],
          '#attributes' => [
            'class' => [
              'tmgmt-review-message-inline',
            ],
          ],
          '#ajax' => [
            'callback' => '::ajaxReviewForm',
            'wrapper' => $ajax_id,
          ],
        ];
      }
    }
  }
  return $item_element;
}