You are here

public function ConflictResolutionDialogFormBuilder::resolveConflictsAjax in Conflict 8.2

Ajax callback returning the UI for conflict resolution.

Parameters

$form: The form array.

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

Return value

\Drupal\Core\Ajax\AjaxResponse The ajax response containing the conflict resolution UI.

File

src/Form/ConflictResolutionDialogFormBuilder.php, line 164

Class

ConflictResolutionDialogFormBuilder

Namespace

Drupal\conflict\Form

Code

public function resolveConflictsAjax($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  if ($form_state
    ->get('conflict.first_processing')) {
    $response
      ->addCommand(new CloseDialogCommand('#conflict-overview-form', TRUE));
  }
  $path = $form_state
    ->get('conflict.processing_path');
  if (!is_null($path)) {
    $build = [];
    $path_titles = [];
    $path_entities = static::getEntitiesForPropertyPath($path, $form_state);
    foreach ($path_entities as $path_entity) {
      $path_titles[] = $path_entity
        ->getEntityType()
        ->getLabel() . ': "' . $path_entity
        ->label() . '"';
    }
    $path_entity = end($path_entities);
    $conflict_ui_title = t('Resolving conflicts in: @entity_type "@entity"', [
      '@entity_type' => $path_entity
        ->getEntityType()
        ->getLabel(),
      '@entity' => $path_entity
        ->label(),
    ]);
    $build['conflict_path_preview'] = [
      '#type' => 'details',
      '#title' => t('Show conflict path'),
      '#open' => FALSE,
      'preview' => static::buildNestedItemList($path_titles),
    ];
    $build['conflict_resolution_resolve_conflicts'] = [
      '#type' => 'submit',
      '#name' => 'conflict_resolution_resolve_conflicts',
      '#value' => empty($form_state
        ->get('conflict.paths')) ? t('Finish conflict resolution') : t('Go to the next conflict'),
      '#weight' => 1000,
      '#attributes' => [
        'class' => [
          'conflict-resolve-conflicts',
        ],
      ],
    ];
    $this
      ->getEntityConflictUIResolver($path_entity
      ->getEntityTypeId())
      ->addConflictResolution($path, $form_state, $path_entity, $build, $response);
    $options = [
      'dialogClass' => 'conflict-resolution-dialog conflict-resolution-dialog-step',
      'closeOnEscape' => FALSE,
      'resizable' => TRUE,
      'draggable' => TRUE,
      'width' => 'auto',
    ];
    $cmd = new OpenModalDialogCommand($conflict_ui_title, $build, $options);
    $response
      ->addCommand($cmd);
  }
  else {
    $close_modal_cmd = new CloseModalDialogCommand();
    $response
      ->addCommand($close_modal_cmd);
  }

  // @todo exchange the original entity's hash through a command
  return $response;
}