You are here

public function UpdateForm::submitForm in Workspace 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormInterface::submitForm

File

src/Form/UpdateForm.php, line 172

Class

UpdateForm
The form to update the current workspace with its upstream.

Namespace

Drupal\workspace\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $upstream = $this
    ->getUpstream();
  $active = $this
    ->getActive();
  try {

    // Derive a replication task from the Workspace we are acting on.
    $task = $this->replicatorManager
      ->getTask($active
      ->getWorkspace(), 'pull_replication_settings');
    $response = $this->replicatorManager
      ->update($upstream, $active, $task);
    if ($response instanceof ReplicationLogInterface && $response
      ->get('ok')->value == TRUE) {

      // Notify the user if there are now conflicts.
      $conflicts = $this->conflictTracker
        ->useWorkspace($active
        ->getWorkspace())
        ->getAll();
      if ($conflicts) {
        drupal_set_message($this
          ->t('%workspace has been updated with content from %upstream, but there are <a href=":link">@count conflict(s) with the %target workspace</a>.', [
          '%upstream' => $upstream
            ->label(),
          '%workspace' => $active
            ->label(),
          ':link' => Url::fromRoute('entity.workspace.conflicts', [
            'workspace' => $active
              ->getWorkspace()
              ->id(),
          ])
            ->toString(),
          '@count' => count($conflicts),
          '%target' => $upstream
            ->label(),
        ]), 'error');
      }
      else {
        drupal_set_message($this
          ->t('An update of %workspace has been queued with content from %upstream.', [
          '%upstream' => $upstream
            ->label(),
          '%workspace' => $active
            ->label(),
        ]));
        if (\Drupal::moduleHandler()
          ->moduleExists('deploy')) {
          $input = $form_state
            ->getUserInput();
          if (!isset($input['_drupal_ajax'])) {
            $form_state
              ->setRedirect('entity.replication.collection');
          }
        }
      }
    }
    else {
      drupal_set_message($this
        ->t('Error updating %workspace from %upstream.', [
        '%upstream' => $upstream
          ->label(),
        '%workspace' => $active
          ->label(),
      ]), 'error');
    }
  } catch (\Exception $e) {
    watchdog_exception('Workspace', $e);
    drupal_set_message($e
      ->getMessage(), 'error');
  }
}